我想使用我的vuex模块作为类,以使我的代码更整洁和可读性更好。我使用了本文档底部的小节(使用NuxtJS访问模块):https://github.com/championswimmer/vuex-module-decorators/blob/master/README.md
我已经搜索了近3天的解决方案,并尝试了这个链接:vuex not loading module decorated with vuex-module-decorators,但它不起作用。另外,我在组件中直接使用了getModule,就像这个问题页面中的解决方案:https://github.com/championswimmer/vuex-module-decorators/issues/80
import CounterModule from '../store/modules/test_module';
import { getModule } from 'vuex-module-decorators';
let counterModule: CounterModule;然后
created() {
counterModule = getModule(CounterModule, this.$store);
}然后,在别处访问方法
computed: {
counter() {
return counterModule.getCount
}
}它对我不起作用!
这是Nuxtjs项目中store文件夹中的my Module:
import { ICurrentUser } from '~/models/ICurrentUser'
import { Module, VuexModule, Mutation, MutationAction } from 'vuex-module-decorators'
@Module({ stateFactory: true, namespaced: true, name: 'CurrentUserStore' })
export default class CurrentUser extends VuexModule {
user: ICurrentUser = {
DisplayName: null,
UserId: null,
};
@Mutation
setUser(userInfo: ICurrentUser) {
this.user = userInfo;
}
get userInfo() {
return this.user;
}
}在sore文件夹的index.ts文件中:
import { Store } from 'vuex'
import { getModule } from 'vuex-module-decorators'
import CurrentUser from './currentUser'
let currentUserStore: CurrentUser
const initializer = (store: Store<any>): void => {
debugger
currentUserStore = getModule(CurrentUser, store)
}
export const plugins = [initializer]
export {
currentUserStore,
}我认为问题源于这一行:
currentUserStore = getModule(CurrentUser, store)currentUserStore被创建为对象,但无法识别属性和方法。
当我想要使用getter或突变时,我得到了错误。例如,使用突变时的“未知突变类型”
发布于 2020-04-03 18:15:42
可能晚了几个月,但我遇到了类似的问题,最终在https://github.com/championswimmer/vuex-module-decorators/issues/179中找到了解决方案
它谈到了多个需求(总结为elsewhere)
与此问题相关的一个问题是,模块的文件名必须与您在@Module定义中指定的名称相匹配。
在您的情况下,如果您的文件从currentUser重命名为CurrentUserStore' or change the name of the module toCurrentUser`,应该可以解决这个问题。
https://stackoverflow.com/questions/57195346
复制相似问题