2021-03-15 12:44:58 +08:00
|
|
|
import { InjectionKey } from 'vue';
|
2021-06-19 17:49:42 +08:00
|
|
|
import { createStore, useStore as baseUseStore, Store } from 'vuex';
|
2021-05-04 20:51:36 +08:00
|
|
|
import { RootStateTypes } from '/@/store/interface/index';
|
2021-06-16 17:32:36 +08:00
|
|
|
|
2021-06-20 11:46:51 +08:00
|
|
|
// Vite supports importing multiple modules from the file system using the special import.meta.glob function
|
|
|
|
// see https://cn.vitejs.dev/guide/features.html#glob-import
|
|
|
|
const modulesFiles = import.meta.globEager('./modules/*.ts');
|
|
|
|
const pathList: string[] = [];
|
2021-06-19 17:49:42 +08:00
|
|
|
|
2021-06-20 11:46:51 +08:00
|
|
|
for (const path in modulesFiles) {
|
|
|
|
pathList.push(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
const modules = pathList.reduce((modules: { [x: string]: any }, modulePath: string) => {
|
|
|
|
const moduleName = modulePath.replace(/^\.\/modules\/(.*)\.\w+$/, '$1');
|
|
|
|
const value = modulesFiles[modulePath];
|
|
|
|
modules[moduleName] = value.default;
|
|
|
|
return modules;
|
|
|
|
}, {});
|
2021-01-05 18:11:13 +08:00
|
|
|
|
2021-03-15 12:44:58 +08:00
|
|
|
export const key: InjectionKey<Store<RootStateTypes>> = Symbol();
|
2021-01-05 18:11:13 +08:00
|
|
|
|
2021-06-20 11:46:51 +08:00
|
|
|
export const store = createStore<RootStateTypes>({ modules });
|
2021-01-05 18:11:13 +08:00
|
|
|
|
|
|
|
export function useStore() {
|
2021-03-15 12:44:58 +08:00
|
|
|
return baseUseStore(key);
|
|
|
|
}
|