ruoyi-plus-vben5/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 appapp缓存冲突
*
*/
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 };