I'm having a hard time making my vuex modules work with NuxtJS SSR, TypeScript and vuex-module-decorators.
I tried following guide from the official nuxt website and the vuex-module-decorators, but nothing works...
Here's my code :
// 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
}
}
Whenever I try to access anything in my modules, these are undefined...
I know this approach is "static modules". My goal is to use dynamic modules, but if the static modules are the only way to make this work with NuxtJS SSR, it'll be fine.
Any help will be greatly appreciated. Thanks :)
EDIT
I gave up and used vuex-class-component