我在 vuejs 2.9 版上工作,我想在页面刷新时保留我的商店。
我使用 pinia 并喜欢在线文档:https://prazdevs.github.io/pinia-plugin-persistedstate/guide/
main.ts:
import { createApp } from "vue";
import App from "./App.vue";
import { createPinia } from "pinia";
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate';
const app = createApp(App);
app.use(createPinia());
const pinia = createPinia()
pinia.use(piniaPluginPersistedstate);
app.use(router);
app.mount("#app");
我的店铺:
import { defineStore } from "pinia";
export const TokenStore = defineStore({
id: "token",
state: () => ({
email:"",
pseudo: "",
myid: "",
nom:"",
prenom:"",
role: "",
token:"",
avatar:"",
}),
persist: true,
});
一个想法为什么行不通?或者其他方式来保留我的商店?
我认为错误是在这些行中的应用程序的入口点:
app.use(createPinia()); // this is using one instance of pinia
const pinia = createPinia() // here you are creating a second instance of pinia
pinia.use(piniaPluginPersistedstate) // here you are applying a package to the second instance that is not the one which is connected to the vue app.
你能试试这个吗
app.use(pinia) // after the pinia.use declaration