我想实现一个功能,可以在组件创建/销毁时注册和注销一些模块,我的测试如下所示:
// ../store/DMRTest.ts
const DMRTest = {
state: { name: 'DMR test ' },
mutations: {
getName(state) {
state.name += state.name;
}
},
actions: {},
getters: { NN(state) { return state.name; } },
};
export default DMRTest;
// App.vue
import DMRTest from '../store/DMRTest.ts';
// ...
async created() {
// Not works!
this.$store.registerModule('DMRTest', await import('../store/DMRTest.ts'));
// Works
this.$store.registerModule('DMRTest', DMRTest);
console.log(this.$store);
console.log(this.$store.state);
}
当我静态导入模块时,它将被正确注册:

但是当我使用import函数时,商店像这样注册了模块:

如何动态导入模块?
发布于 2019-12-26 16:49:09
我应该将default附加到动态导入的问题结束之后
发布于 2022-02-05 05:09:32
就像用一个例子写的那样:
const store = new createStore(rootStoreModule);
const modules = ['moduleA', 'moduleB']
for(const module of modules) {
import(`./components/${ module.toLowerCase() }/store.js`)
.then( (obj) => { store.registerModule(module, obj.default); } )
}https://stackoverflow.com/questions/59485884
复制相似问题