Files
lgp-admin-plus/packages/stores/src/setup.ts

36 lines
875 B
TypeScript
Raw Normal View History

2024-05-19 21:20:42 +08:00
import type { App } from 'vue';
import { createPinia } from 'pinia';
interface SetupStoreOptions {
2024-05-24 23:11:03 +08:00
/**
* @zh_CN 环境
*/
env: string;
2024-05-19 21:20:42 +08:00
/**
* @zh_CN 应用名,由于 @vben/stores 是公用的,后续可能有多个app,为了防止多个app缓存冲突,可在这里配置应用名
* 应用名将被用于持久化的前缀
*/
2024-05-24 23:11:03 +08:00
namespace: string;
2024-05-19 21:20:42 +08:00
}
/**
* @zh_CN 初始化pinia
* @param app vue app 实例
*/
2024-05-24 23:11:03 +08:00
async function setupStore(app: App, options: SetupStoreOptions) {
2024-05-19 21:20:42 +08:00
const { createPersistedState } = await import('pinia-plugin-persistedstate');
const pinia = createPinia();
2024-05-24 23:11:03 +08:00
const { env, namespace } = options;
2024-05-19 21:20:42 +08:00
pinia.use(
createPersistedState({
// key $appName-$store.id
2024-05-24 23:11:03 +08:00
key: (storeKey) => `__${namespace}-${env}-${storeKey}__`,
2024-05-19 21:20:42 +08:00
storage: localStorage,
}),
);
app.use(pinia);
}
export { setupStore };