.
This commit is contained in:
@@ -1,24 +1,25 @@
|
||||
import {InjectionKey} from 'vue'
|
||||
import {createStore,useStore as baseUseStore ,Store} from 'vuex'
|
||||
import {RootStateTypes} from "@store/interface";
|
||||
|
||||
export interface State {
|
||||
count: number
|
||||
// Vite 使用特殊的 import.meta.glob 函数从文件系统导入多个模块
|
||||
// see https://cn.vitejs.dev/guide/features.html#glob-import
|
||||
const moduleFiles = import.meta.globEager('./modules/*.ts')
|
||||
const paths:string[]=[]
|
||||
|
||||
for (const path in moduleFiles) {
|
||||
paths.push(path)
|
||||
}
|
||||
|
||||
export const key: InjectionKey<Store<State>> = Symbol()
|
||||
const modules = paths.reduce((modules: { [x: string]: any }, modulePath: string) => {
|
||||
const moduleKey = modulePath.replace(/^\.\/modules\/(.*)\.\w+$/, '$1');
|
||||
modules[moduleKey] = moduleFiles[modulePath].default;
|
||||
return modules;
|
||||
}, {});
|
||||
|
||||
export const store = createStore<State>({
|
||||
state() {
|
||||
return {
|
||||
count: 0
|
||||
}
|
||||
},
|
||||
mutations: {
|
||||
increment(state: { count: number }) {
|
||||
state.count++
|
||||
}
|
||||
}
|
||||
})
|
||||
export const key: InjectionKey<Store<RootStateTypes>> = Symbol()
|
||||
|
||||
export const store = createStore<RootStateTypes>({modules})
|
||||
|
||||
export function userStore(){
|
||||
return baseUseStore(key)
|
||||
|
||||
Reference in New Issue
Block a user