feat(projects): add pinia setup syntax example: setup-store[添加setup syntax的pinia示例setup-store]

This commit is contained in:
Soybean 2022-09-21 18:14:57 +08:00
parent a539112a0f
commit 82c4b09b94
4 changed files with 51 additions and 0 deletions

View File

@ -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);
}

View File

@ -3,3 +3,4 @@ export * from './theme';
export * from './auth';
export * from './tab';
export * from './route';
export * from './setup-store';

View File

@ -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<Config>({ name: 'config' });
/** 设置配置 */
function setConfig(conf: Partial<Config>) {
Object.assign(config, conf);
}
return {
visible,
show,
hide,
config,
setConfig
};
});

View File

@ -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);
};
}
}