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

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

View File

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