Skip to content

Commit 9c8ba07

Browse files
committed
feat: Added persistent Pinia state demo
1 parent 564cede commit 9c8ba07

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

src/stores/modules/counter.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { defineStore } from 'pinia'
2+
3+
const useCounterStore = defineStore('counter', () => {
4+
const counter = ref(0)
5+
6+
const increment = () => {
7+
counter.value++
8+
}
9+
10+
return {
11+
counter,
12+
increment,
13+
}
14+
}, {
15+
persist: true,
16+
})
17+
18+
export default useCounterStore

src/typed-router.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ declare module 'vue-router/auto/routes' {
4141
export interface RouteNamedMap {
4242
'index': RouteRecordInfo<'index', '/', Record<never, never>, Record<never, never>>,
4343
'charts': RouteRecordInfo<'charts', '/charts', Record<never, never>, Record<never, never>>,
44+
'counter': RouteRecordInfo<'counter', '/counter', Record<never, never>, Record<never, never>>,
4445
'mock': RouteRecordInfo<'mock', '/mock', Record<never, never>, Record<never, never>>,
4546
'unocss': RouteRecordInfo<'unocss', '/unocss', Record<never, never>, Record<never, never>>,
4647
}

src/views/counter/index.vue

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<script setup lang="ts">
2+
import { storeToRefs } from 'pinia'
3+
import useCounterStore from '@/stores/modules/counter'
4+
5+
definePage({
6+
name: 'counter',
7+
meta: {
8+
level: 2,
9+
},
10+
})
11+
12+
const counterStore = useCounterStore()
13+
const { counter } = storeToRefs(counterStore)
14+
15+
function add() {
16+
counterStore.increment()
17+
}
18+
19+
// back
20+
const onClickLeft = () => history.back()
21+
</script>
22+
23+
<template>
24+
<div>
25+
<VanNavBar title="🍍 持久化 Pinia 状态" left-arrow fixed @click-left="onClickLeft" />
26+
27+
<div class="h-100vh w-full px-20 py-60">
28+
<h1 class="text-6xl color-pink font-semibold">
29+
Hello, Pinia!
30+
</h1>
31+
<p class="mt-4 text-gray-700 dark:text-white">
32+
This is a simple example of persisting Pinia state.
33+
To verify its effectiveness, you can refresh the interface and observe it.
34+
</p>
35+
<p class="mt-4">
36+
number:<strong class="text-green-500"> {{ counter }} </strong>
37+
</p>
38+
<button class="btn border-none btn-green" @click="add">
39+
Add
40+
</button>
41+
</div>
42+
</div>
43+
</template>

src/views/index.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const checked = ref<boolean>(false)
2424

2525
<VanCell title="🪶 Unocss 示例" to="unocss" is-link />
2626

27+
<VanCell title="🍍 持久化 Pinia 状态" to="counter" is-link />
28+
2729
<VanCell center>
2830
<template #title>
2931
<span class="custom-title">🎨 欢迎补充</span>

0 commit comments

Comments
 (0)