【发布时间】:2020-06-05 23:06:27
【问题描述】:
我很难让我的 vuex 模块与 NuxtJS SSR、TypeScript 和 vuex-module-decorators 一起使用。
我尝试关注guide from the official nuxt website 和vuex-module-decorators,但没有任何效果...
这是我的代码:
// store/CommonModule.ts
import { Module, VuexModule, Mutation } from 'vuex-module-decorators'
@Module({
name: 'CommonModule',
namespaced: true,
stateFactory: true,
})
export default class CommonModule extends VuexModule {
message: string = 'hi'
@Mutation
public SET_MESSAGE(val: string) {
this.message = val
}
}
// store/index.ts
import Vuex from 'vuex'
import CommonModule from '~/store/CommonModule'
export function createStore() {
return new Vuex.Store({
modules: {
CommonModule,
},
})
}
// components/Home.vue
import { Component, Vue } from 'vue-property-decorator';
import { getModule } from 'vuex-module-decorators';
import CommonModule from '~/store/CommonModule'
@Component
export default class Home extends Vue {
get message(): string {
const commonModule = getModule(CommonModule, this.$store)
return commonModule.message // throws an error: Cannot read property 'message' of undefined
}
}
每当我尝试访问模块中的任何内容时,这些都是未定义的...
我知道这种方法是“静态模块”。我的目标是使用动态模块,但如果静态模块是使用 NuxtJS SSR 的唯一方法,那就没问题了。
任何帮助将不胜感激。谢谢:)
编辑
我放弃了,用vuex-class-component
【问题讨论】:
-
你最终找到答案了吗?
-
@MikeOttink 不。正如我的问题中提到的,我放弃并使用了 vuex-class-component。
-
也放弃了使用vuex-class-component。像魅力一样工作,并且只有一小部分样板。
标签: typescript vue.js vuex nuxt.js