feat: Add persist option to useCounterStore
All checks were successful
CI / cache-and-install (push) Successful in 1m36s

This commit is contained in:
严浩
2024-09-11 10:47:54 +08:00
parent 3a8c685510
commit 8b4b361c7c
4 changed files with 577 additions and 257 deletions

View File

@ -27,7 +27,7 @@
"nprogress": "^0.2.0", "nprogress": "^0.2.0",
"page-stack-vue3": "^2.5.6", "page-stack-vue3": "^2.5.6",
"pinia": "^2.2.2", "pinia": "^2.2.2",
"pinia-plugin-persistedstate": "^3.2.3", "pinia-plugin-persistedstate": "^4.0.1",
"radash": "^12.1.0", "radash": "^12.1.0",
"tdesign-icons-vue-next": "^0.2.6", "tdesign-icons-vue-next": "^0.2.6",
"tdesign-mobile-vue": "^1.4.1", "tdesign-mobile-vue": "^1.4.1",
@ -67,7 +67,7 @@
"lint-staged": "^15.2.10", "lint-staged": "^15.2.10",
"npm-run-all2": "^6.2.2", "npm-run-all2": "^6.2.2",
"prettier": "^3.3.3", "prettier": "^3.3.3",
"typescript": "~5.5.4", "typescript": "~5.6.2",
"unocss": "^0.62.3", "unocss": "^0.62.3",
"unplugin-auto-import": "^0.18.2", "unplugin-auto-import": "^0.18.2",
"unplugin-icons": "^0.19.3", "unplugin-icons": "^0.19.3",
@ -81,11 +81,7 @@
"vitest": "^2.0.5", "vitest": "^2.0.5",
"vue-tsc": "^2.1.6" "vue-tsc": "^2.1.6"
}, },
"pnpm": { "pnpm": {},
"patchedDependencies": {
"vue-router@4.4.3": "patches/vue-router@4.4.3.patch"
}
},
"lint-staged": { "lint-staged": {
"src/**/*.{js,ts,vue}": [ "src/**/*.{js,ts,vue}": [
"prettier --write", "prettier --write",

794
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -1,13 +1,13 @@
import './assets/main.css'; import './assets/main.css';
import 'virtual:uno.css'; import 'virtual:uno.css';
import { createApp } from 'vue';
import { createPinia } from 'pinia';
import { createHead } from '@unhead/vue'; import { createHead } from '@unhead/vue';
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate';
import { DataLoaderPlugin } from 'unplugin-vue-router/data-loaders';
import App from './App.vue'; import App from './App.vue';
import { router } from './router'; import { router } from './router';
import { DataLoaderPlugin } from 'unplugin-vue-router/data-loaders';
async function init() { async function init() {
if (import.meta.env.MODE === 'development' || 1 === 1) { if (import.meta.env.MODE === 'development' || 1 === 1) {
@ -25,7 +25,7 @@ async function init() {
const app = createApp(App) const app = createApp(App)
.use(createHead()) .use(createHead())
.use(createPinia()) .use(createPinia().use(piniaPluginPersistedstate))
// Register the plugin before the router // Register the plugin before the router
.use(DataLoaderPlugin, { router }) .use(DataLoaderPlugin, { router })
// adding the router will trigger the initial navigation // adding the router will trigger the initial navigation

View File

@ -1,7 +1,9 @@
import { ref, computed } from 'vue'; import { ref, computed } from 'vue';
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
export const useCounterStore = defineStore('counter', () => { export const useCounterStore = defineStore(
'counter',
() => {
const count = ref(0); const count = ref(0);
const doubleCount = computed(() => count.value * 2); const doubleCount = computed(() => count.value * 2);
function increment() { function increment() {
@ -9,4 +11,8 @@ export const useCounterStore = defineStore('counter', () => {
} }
return { count, doubleCount, increment }; return { count, doubleCount, increment };
}); },
{
persist: true,
},
);