diff --git a/src/store/index.ts b/src/store/index.ts index 74cdcb051..f85ab1fc1 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -1,9 +1,12 @@ import type { App } from 'vue'; import { createPinia } from 'pinia'; +import { resetSetupStore } from './plugins'; /** setup vue store plugin: pinia. - [安装vue状态管理插件:pinia] */ export function setupStore(app: App) { const store = createPinia(); + store.use(resetSetupStore); + app.use(store); } diff --git a/src/store/modules/index.ts b/src/store/modules/index.ts index 1c7aa7cf9..b7bf4b50e 100644 --- a/src/store/modules/index.ts +++ b/src/store/modules/index.ts @@ -3,3 +3,4 @@ export * from './theme'; export * from './auth'; export * from './tab'; export * from './route'; +export * from './setup-store'; diff --git a/src/store/modules/setup-store/index.ts b/src/store/modules/setup-store/index.ts new file mode 100644 index 000000000..ac3a6c291 --- /dev/null +++ b/src/store/modules/setup-store/index.ts @@ -0,0 +1,26 @@ +import { reactive } from 'vue'; +import { defineStore } from 'pinia'; +import { useBoolean } from '@/hooks'; + +export const useSetupStore = defineStore('setup-store', () => { + const { bool: visible, setTrue: show, setFalse: hide } = useBoolean(); + + interface Config { + name: string; + } + + const config = reactive({ name: 'config' }); + + /** 设置配置 */ + function setConfig(conf: Partial) { + Object.assign(config, conf); + } + + return { + visible, + show, + hide, + config, + setConfig + }; +}); diff --git a/src/store/plugins/index.ts b/src/store/plugins/index.ts new file mode 100644 index 000000000..1b43f5bbb --- /dev/null +++ b/src/store/plugins/index.ts @@ -0,0 +1,21 @@ +import type { PiniaPluginContext } from 'pinia'; +import { cloneDeep } from 'lodash-es'; + +/** + * setup语法的重置状态插件 + * @param context + * @description 请将用setup语法的状态id写入到setupSyntaxIds + */ +export function resetSetupStore(context: PiniaPluginContext) { + const setupSyntaxIds = ['setup-store']; + + if (setupSyntaxIds.includes(context.store.$id)) { + const { $state } = context.store; + + const defaultStore = cloneDeep($state); + + context.store.$reset = () => { + context.store.$patch(defaultStore); + }; + } +}