Skip to content

Commit 622681b

Browse files
committed
chore: example about vuex
1 parent d4f87b7 commit 622681b

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-1
lines changed

playground/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"pinia": "^2.0.23",
1515
"vue": "^3.2.41",
1616
"vue-router": "^4.1.5",
17-
"vuefire": "workspace:*"
17+
"vuefire": "workspace:*",
18+
"vuex": "^4.1.0"
1819
},
1920
"devDependencies": {
2021
"@firebase/app-types": "^0.8.0",

playground/src/main.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,27 @@ import { firestorePlugin } from 'vuefire'
44
import App from './App.vue'
55
import { createFirebaseApp, VueFirePlugin } from './firebase'
66
import { createWebHistory, createRouter } from 'vue-router/auto'
7+
import { createStore } from 'vuex'
78

89
const router = createRouter({
910
history: createWebHistory(),
1011
})
1112

13+
const store = createStore({
14+
// can't work with vuefire
15+
// strict: import.meta.env.DEV,
16+
state: () => ({
17+
count: 0,
18+
todos: [],
19+
}),
20+
})
21+
1222
const app = createApp(App)
1323
app
1424
.use(createPinia())
1525
.use(firestorePlugin)
1626
.use(VueFirePlugin(createFirebaseApp()))
27+
.use(store)
1728
.use(router)
1829

1930
app.mount('#app')

playground/src/pages/vuex-store.vue

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<script lang="ts" setup>
2+
import { useFirestore } from '@/firebase'
3+
import { doc, setDoc } from 'firebase/firestore'
4+
import { toRef } from 'vue'
5+
import { useDocument } from 'vuefire'
6+
import { useStore } from 'vuex'
7+
8+
const store = useStore()
9+
const db = useFirestore()
10+
const countRef = doc(db, 'playground', 'pinia-counter').withConverter<number>({
11+
toFirestore(n) {
12+
return { n }
13+
},
14+
fromFirestore(snapshot) {
15+
return snapshot.data().n as number
16+
},
17+
})
18+
19+
const count = toRef(store.state, 'count')
20+
21+
useDocument(countRef, {
22+
target: count,
23+
})
24+
25+
async function increment() {
26+
await setDoc(countRef, count.value + 1)
27+
}
28+
</script>
29+
30+
<template>
31+
<div>
32+
<p>count: {{ count }}</p>
33+
<button @click="increment">increment</button>
34+
</div>
35+
</template>

playground/typed-router.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ declare module 'vue-router/auto/routes' {
7272
Record<never, never>,
7373
Record<never, never>
7474
>
75+
'/vuex-store': RouteRecordInfo<
76+
'/vuex-store',
77+
'/vuex-store',
78+
Record<never, never>,
79+
Record<never, never>
80+
>
7581
}
7682
}
7783

pnpm-lock.yaml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)