pinia实例 如何清理,持久层设置的是localstorage
,localstorage.removeItem
清空key之后 pinia实例里面还是有,如何清理pinia实例里面的
export const useOpenXLibrary = defineStore({
id: 'useOpenXLibrary', //
state: () => {
return {
OpenXLibrary: {}
};
},
getters: {
getOpenXLibrary(): Object {
return this.OpenXLibrary;
}
},
actions: {
updateOpenXLibrary(OpenXLibrary: Object) {
this.OpenXLibrary = OpenXLibrary;
},
clearOpenXLibraryStore() {
this.OpenXLibrary = {};
}
},
persist: true
});
// 这是清空localStorage
function clearLocalStorage() {
const keys = ['useToken', 'useOpenXIdAdd', 'useOpenXLibrary'];
for (let i = localStorage.length - 1; i >= 0; i--) {
const key = localStorage.key(i) as string;
if (!keys.includes(key)) {
localStorage.removeItem(key);
}
}
ElMessage.success('????');
router.push({ path: '/' });
}
我是意思想所有的store里面 里面的数据全部clear 如 state里的 OpenXLibrary: {}、ids: []
不是直接调用
localstorage.removeItem()
来手动删除localstorage
中的值。而是改写你的clearOpenXLibraryStore
方法,清空Pinia
的值的同时去同步删除localstorage
中对应的内容:没有用到
vue-use
库,如果你用了 useStorage 会更简单一些。