【问题标题】:Nuxtjs using Vuex-module-decorator doesn't wordkNuxtjs 使用 Vuex-module-decorator 不会说话
【发布时间】:2019-12-03 08:11:12
【问题描述】:

我想将我的 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 文件夹中的模块:

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 或突变时,我会出错。例如,使用突变的“未知突变类型”

【问题讨论】:

标签: vue.js vuejs2 vuex nuxt.js


【解决方案1】:

可能晚了几个月,但我遇到了类似的问题,最终在https://github.com/championswimmer/vuex-module-decorators/issues/179找到了解决方案

它谈到了多个需求(总结elsewhere)

与此问题相关的是模块的文件名必须与您在@Module 定义中指定的名称匹配。

在您的情况下,如果您将文件从 currentUser 重命名为 CurrentUserStore' or change the name of the module toCurrentUser`,它应该可以解决问题。

【讨论】:

    猜你喜欢
    • 2020-06-05
    • 2020-11-05
    • 1970-01-01
    • 2021-01-27
    • 2019-09-16
    • 2020-01-05
    • 1970-01-01
    • 2019-09-14
    • 2018-03-25
    相关资源
    最近更新 更多