简体   繁体   English

使用 Vuex-module-decorator 的 Nuxtjs 不会说话

[英]Nuxtjs using Vuex-module-decorator doesn't wordk

I want to use my vuex modules as classes to make my code more clean and readable.我想将我的 vuex 模块用作类,以使我的代码更清晰易读。 I used the section (Accessing modules with NuxtJS) at the bottom of this document: https://github.com/championswimmer/vuex-module-decorators/blob/master/README.md我使用了本文档底部的部分(使用 NuxtJS 访问模块): https : //github.com/championswimmer/vuex-module-decorators/blob/master/README.md

I've searched for the solution for almost 3 days and tried out this link: vuex not loading module decorated with vuex-module-decorators but, it didn't work.我已经搜索了将近 3 天的解决方案并尝试了这个链接: vuex not loading module modified with vuex-module-decorators但是,它没有用。 Also, I used getModule directly in the component like the solution in this issue page: https://github.com/championswimmer/vuex-module-decorators/issues/80另外,我直接在组件中使用了 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;

Then然后

created() {
        counterModule = getModule(CounterModule, this.$store);
}

Then, accessing method elsewhere然后,在别处访问方法

computed: {
        counter() {
            return counterModule.getCount
        }
    }

it didn't work for me!它对我不起作用!

This is my Module in store folder in Nuxtjs project:这是我在 Nuxtjs 项目中 store 文件夹中的模块:

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;
    }
}

In index.ts file in sore folder:在 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,
}

I think the problem stems from this line:我认为问题源于这一行:

currentUserStore = getModule(CurrentUser, store)

currentUserStore is created as object but properties and methods are not recognizable. currentUserStore 作为对象创建,但无法识别属性和方法。

when I want to use getters or mutation I get error.当我想使用 getter 或突变时,我会出错。 For instance, "unknown mutation type" for using mutation例如,使用突变的“未知突变类型”

相同的问题:-(看起来还没有准备好使用。

Probably several months late but I struggled with a similar issue, and eventually found the solution in https://github.com/championswimmer/vuex-module-decorators/issues/179可能晚了几个月,但我遇到了类似的问题,最终在https://github.com/championswimmer/vuex-module-decorators/issues/179 中找到了解决方案

It talks about multiple requirements (which are summarised elsewhere )它讨论了多个需求(在别处总结)

The one that relates to this issue is that the file name of the module has to match the name you specify in the @Module definition.与此问题相关的一个是模块的文件名必须与您在@Module定义中指定的名称相匹配。

In your case, if you rename your file from currentUser to CurrentUserStore' or change the name of the module to CurrentUser`, it should fix the issue.在您的情况下,如果您将文件从currentUser重命名为CurrentUserStore' or change the name of the module to CurrentUser`,则应该可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM