首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Nuxtjs使用Vuex-module-decorator不工作

Nuxtjs使用Vuex-module-decorator不工作
EN

Stack Overflow用户
提问于 2019-07-25 14:08:05
回答 1查看 1.2K关注 0票数 1

我想使用我的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

代码语言:javascript
复制
import CounterModule from '../store/modules/test_module';
import { getModule } from 'vuex-module-decorators';
let counterModule: CounterModule;

然后

代码语言:javascript
复制
created() {
        counterModule = getModule(CounterModule, this.$store);
}

然后,在别处访问方法

代码语言:javascript
复制
computed: {
        counter() {
            return counterModule.getCount
        }
    }

它对我不起作用!

这是Nuxtjs项目中store文件夹中的my Module:

代码语言:javascript
复制
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文件中:

代码语言:javascript
复制
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,
}

我认为问题源于这一行:

代码语言:javascript
复制
currentUserStore = getModule(CurrentUser, store)

currentUserStore被创建为对象,但无法识别属性和方法。

当我想要使用getter或突变时,我得到了错误。例如,使用突变时的“未知突变类型”

EN

回答 1

Stack Overflow用户

发布于 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`,应该可以解决这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57195346

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档