ruoyi-plus-vben5/packages/stores/src/setup.ts

33 lines
907 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 {
/**
* @zh_CN , @vben/stores appapp缓存冲突
*
*/
cachePrefix?: string;
}
/**
* @zh_CN pinia
* @param app vue app
*/
async function setupStore(app: App, options: SetupStoreOptions = {}) {
const { createPersistedState } = await import('pinia-plugin-persistedstate');
const pinia = createPinia();
const { cachePrefix = 'vben-admin-pro' } = options;
const env = import.meta.env.DEV ? 'dev' : 'prod';
pinia.use(
createPersistedState({
// key $appName-$store.id
key: (storeKey) => `__${cachePrefix}-${storeKey}-${env}__`,
storage: localStorage,
}),
);
app.use(pinia);
}
export { setupStore };