Some checks failed
CI / Test (ubuntu-latest) (push) Has been cancelled
CI / Test (windows-latest) (push) Has been cancelled
CI / Lint (ubuntu-latest) (push) Has been cancelled
CI / Lint (windows-latest) (push) Has been cancelled
CI / Check (ubuntu-latest) (push) Has been cancelled
CI / Check (windows-latest) (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
Deploy Website on push / Deploy Push Playground Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Docs Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Antd Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Element Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Naive Ftp (push) Has been cancelled
Release Drafter / update_release_draft (push) Has been cancelled
CI / CI OK (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Issue Close Require / close-issues (push) Has been cancelled
Close stale issues / stale (push) Has been cancelled
49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
import type { Pinia } from 'pinia';
|
||
|
||
import type { App } from 'vue';
|
||
|
||
import { createPinia } from 'pinia';
|
||
|
||
let pinia: Pinia;
|
||
|
||
export interface InitStoreOptions {
|
||
/**
|
||
* @zh_CN 应用名,由于 @vben/stores 是公用的,后续可能有多个app,为了防止多个app缓存冲突,可在这里配置应用名,应用名将被用于持久化的前缀
|
||
*/
|
||
namespace: string;
|
||
}
|
||
|
||
/**
|
||
* @zh_CN 初始化pinia
|
||
*/
|
||
export async function initStores(app: App, options: InitStoreOptions) {
|
||
const { createPersistedState } = await import('pinia-plugin-persistedstate');
|
||
pinia = createPinia();
|
||
const { namespace } = options;
|
||
pinia.use(
|
||
createPersistedState({
|
||
// key $appName-$store.id
|
||
key: (storeKey) => `${namespace}-${storeKey}`,
|
||
storage: localStorage,
|
||
}),
|
||
);
|
||
app.use(pinia);
|
||
return pinia;
|
||
}
|
||
|
||
export function resetAllStores() {
|
||
if (!pinia) {
|
||
console.error('Pinia is not installed');
|
||
return;
|
||
}
|
||
const allStores = (pinia as any)._s;
|
||
for (const [_key, store] of allStores) {
|
||
try {
|
||
store.$reset();
|
||
} catch (error) {
|
||
console.error('Error resetting store key:', _key);
|
||
console.error('Error resetting store:', error);
|
||
}
|
||
}
|
||
}
|