Skip to content

Commit df77a46

Browse files
committed
feat: add Pinia persist store example
1 parent 8823d87 commit df77a46

File tree

8 files changed

+242
-537
lines changed

8 files changed

+242
-537
lines changed

app/composables/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 useCounter = defineStore('counter', () => {
4+
const count = ref(0)
5+
6+
function increment() {
7+
count.value++
8+
}
9+
10+
return {
11+
count,
12+
increment,
13+
}
14+
}, {
15+
persist: true,
16+
})
17+
18+
export default useCounter

app/locales/en-US.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"language": "📚 Language",
55
"404Demo": "🙅 Page 404 Demo",
66
"unocssExample": "🎨 Unocss example",
7-
"keepAlive": "🧡 KeepAlive Demo"
7+
"keepAlive": "🧡 KeepAlive Demo",
8+
"persistPiniaState": "💾 Persist Pinia State"
89
},
910
"tabbar": {
1011
"home": "Home",
@@ -24,5 +25,10 @@
2425
},
2526
"keepalive_page": {
2627
"label": "The current component will be cached"
28+
},
29+
"counter_page": {
30+
"label": "This is a simple example of persisting Pinia state. To verify its effectiveness, you can refresh the interface and observe it.",
31+
"label_num": "Number",
32+
"btn_add": "Add"
2733
}
2834
}

app/locales/zh-CN.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"language": "📚 语言",
55
"404Demo": "🙅 404页 演示",
66
"unocssExample": "🎨 Unocss 示例",
7-
"keepAlive": "🧡 KeepAlive 演示"
7+
"keepAlive": "🧡 KeepAlive 演示",
8+
"persistPiniaState": "💾 持久化 Pinia 状态"
89
},
910
"tabbar": {
1011
"home": "主页",
@@ -24,5 +25,10 @@
2425
},
2526
"keepalive_page": {
2627
"label": "当前组件将会被缓存"
28+
},
29+
"counter_page": {
30+
"label": "这是一个简单的持久化 Pinia 状态的例子。为了验证其有效性,你可以刷新界面并观察它。",
31+
"label_num": "数字",
32+
"btn_add": "增加"
2733
}
2834
}

app/pages/counter/index.vue

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<script setup lang="ts">
2+
import useCounter from '~/composables/counter'
3+
4+
definePageMeta({
5+
title: '🍍 持久化 Pinia 状态',
6+
i18n: 'menu.persistPiniaState',
7+
})
8+
9+
const counter = useCounter()
10+
11+
function add() {
12+
counter.increment()
13+
}
14+
</script>
15+
16+
<template>
17+
<div>
18+
<h1 class="text-6xl color-pink font-semibold">
19+
Hello, Pinia!
20+
</h1>
21+
22+
<p class="mt-10 text-gray-700 dark:text-white">
23+
{{ $t('counter_page.label') }}
24+
</p>
25+
26+
<p class="mt-10">
27+
{{ $t('counter_page.label_num') }}:
28+
<strong class="text-green-500"> {{ counter.count }} </strong>
29+
</p>
30+
31+
<button class="mt-10 btn" @click="add">
32+
{{ $t('counter_page.btn_add') }}
33+
</button>
34+
</div>
35+
</template>

app/pages/index.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const { locales } = useI18n() as { locales: ComputedRef<LocaleObject[]> }
3737
const menus = computed(() => [
3838
{ title: t('menu.unocssExample'), route: 'unocss' },
3939
{ title: t('menu.keepAlive'), route: 'keepalive' },
40+
{ title: t('menu.persistPiniaState'), route: 'counter' },
4041
{ title: t('menu.404Demo'), route: 'unknown' },
4142
])
4243

nuxt.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default defineNuxtConfig({
99
'@nuxt/eslint',
1010
'@nuxtjs/i18n',
1111
'@pinia/nuxt',
12+
'pinia-plugin-persistedstate/nuxt',
1213
],
1314

1415
experimental: {

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"@nuxtjs/color-mode": "^3.5.1",
1818
"@nuxtjs/i18n": "^8.5.5",
1919
"nuxt": "^3.13.2",
20+
"pinia-plugin-persistedstate": "^4.0.2",
2021
"vue": "^3.5.8",
2122
"vue-router": "^4.4.5"
2223
},
@@ -45,6 +46,14 @@
4546
"allowedVersions": {
4647
"meow": "^12.x"
4748
}
49+
},
50+
"allowedDeprecatedVersions": {
51+
"glob": "*",
52+
"are-we-there-yet": "2",
53+
"gauge": "3",
54+
"inflight": "1",
55+
"npmlog": "5",
56+
"rimraf": "3"
4857
}
4958
},
5059
"simple-git-hooks": {

0 commit comments

Comments
 (0)