
pinia 实例的正确清理方式
在 pinia 中,不能使用 localstorage.removeitem 直接删除 localstorage 中的值来清理 pinia 实例。正确的做法是改写 store 的 actions 方法,在清空 pinia 实例的 state 的同时,同步删除 localstorage 中对应的内容:
// ...省略前面代码段...
actions: {
updateopenxlibrary(openxlibrary: object) {
this.openxlibrary = openxlibrary;
localstorage.setitem('yourlskey', openxlibrary);
},
clearopenxlibrarystore() {
this.openxlibrary = {};
localstorage.removeitem('yourlskey');
},
},
// ...省略后面代码段...这样,当调用 clearopenxlibrarystore 方法时,pinia 实例中的数据和 localstorage 中对应的内容都会被清空。
使用 vue-use 库的更简洁方法
如果你使用了 vue-use 库,可以使用 usestorage 方法来更简洁地实现 pinia 实例的清理:
import { useStorage } from 'vue-use';
// ...省略其他代码...
const { get, set, remove } = useStorage('yourLsKey');
actions: {
updateOpenXLibrary(OpenXLibrary: Object) {
set(OpenXLibrary);
this.OpenXLibrary = OpenXLibrary;
},
clearOpenXLibraryStore() {
remove();
this.OpenXLibrary = {};
},
},










