简体   繁体   English

带有 NuxtJS 和 vuex-module-decorators 的动态 vuex 存储模块

[英]Dynamic vuex store modules with NuxtJS and vuex-module-decorators

I'm having a hard time making my vuex modules work with NuxtJS SSR, TypeScript and vuex-module-decorators.我很难让我的 vuex 模块与 NuxtJS SSR、TypeScript 和 vuex-module-decorators 一起工作。

I tried following guide from the official nuxt website and the vuex-module-decorators , but nothing works...我尝试遵循 官方 nuxt 网站vuex-module-decorators 的指南,但没有任何效果......

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.我的目标是使用动态模块,但如果静态模块是使用 NuxtJS SSR 进行这项工作的唯一方法,那就没问题了。

Any help will be greatly appreciated.任何帮助将不胜感激。 Thanks :)谢谢 :)

EDIT编辑
I gave up and used vuex-class-component我放弃并使用了vuex-class-component

What your doing in your index.ts file is incorrect if your trying to make your store modules register dynamically.如果您试图让您的商店模块动态注册,那么您在 index.ts 文件中所做的事情是不正确的。

Define an empty store in store/index.ts在 store/index.ts 中定义一个空的 store

import Vuex from 'vuex'

export const store = new Vuex.Store<any>({});

Now in your store modules:现在在您的商店模块中:

// store/CommonModule.ts
import { Module, VuexModule, Mutation } from 'vuex-module-decorators'

// import the empty store
import store from '.'

@Module({
  name: 'CommonModule',
  namespaced: true,
  stateFactory: true,

  // add this
  dyamic:true,
  store

})
export default class CommonModule extends VuexModule {
  message: string = 'hi'

  @Mutation
  public SET_MESSAGE(val: string) {
    this.message = val
  }
}

Was fighting with a similar problem and got it to work with code from this page: https://qiita.com/yoshinbo/items/70f109db7c3de4b4a99f正在与类似的问题作斗争,并使其与此页面中的代码一起使用: https : //qiita.com/yoshinbo/items/70f109db7c3de4b4a99f

  • use the store in your module definition在模块定义中使用 store
  • export result of getModule from your module file从模块文件中导出 getModule 结果
  • default export createStore from ~/store/index.ts默认从 ~/store/index.ts 导出 createStore

Well, you need to call the Message() function inside of the created() lifecycle hook.好吧,您需要在 created() 生命周期钩子中调用 Message() 函数。

Currently as per your code, script starts the executing whenever component is Initialise but by that time store has not been in set up due to which calling it inside lifecycle hooks would make more sense.目前,根据您的代码,只要组件初始化,脚本就会开始执行,但到那时尚未设置存储,因为在生命周期挂钩内调用它会更有意义。

public created() {
  get message(): string {
    const commonModule = getModule(CommonModule, this.$store)
    return commonModule.message;
  }
}

Hope this helps!希望这可以帮助!

For anyone who still wants to use vuex-module-decorator :对于仍然想使用vuex-module-decorator任何人:

From the example of Atlasmaybe's question, it was missing a store-accessor.ts file: check the docs从 Atlasmaybe 的问题的例子来看,它缺少一个store-accessor.ts文件: 检查文档

We have to make some adjustments in the existing files, and we'll be using the getModule function in this new one:我们必须对现有文件进行一些调整,我们将在这个新文件中使用getModule函数:

1 - First in our custom module: 1 - 首先在我们的自定义模块中:

// store/CommonModule.ts
import { Module, VuexModule, Mutation } from 'vuex-module-decorators'

@Module({
  // change the name to lower case
  name: 'commonmodule',
  namespaced: true,
  stateFactory: true,
})
// no need to name the class:
export default class extends VuexModule {
  message: string = 'hi'

  @Mutation
  public SET_MESSAGE(val: string) {
    this.message = val
  }
}

2 - Then create a store-accessor.ts file, inside a utils folder: 2 - 然后在utils文件夹中创建一个store-accessor.ts文件:

// utils/store-accessor.ts
import { Store } from 'vuex'
import { getModule } from 'vuex-module-decorators'
import commonmodule from '~/store/CommonModule'

// this is the name we'll use to import the module
let commonModuleStore: commonmodule

function initialiseStores(store: Store<any>): void {
  commonModuleStore = getModule(commonmodule, store)
}

export { initialiseStores, commonModuleStore }

3 - Now at your store index.ts : 3 - 现在在您的商店index.ts

// store/index.ts
import { Store } from 'vuex';
import { initialiseStores } from '~/utils/store-accessor';

const initializer = (store: Store<any>) => initialiseStores(store);

export const plugins = [initializer];

export * from '~/utils/store-accessor';

4 - And finally at our component, we can import the custom store and use it like: 4 - 最后在我们的组件中,我们可以导入自定义商店并使用它:

// components/Home.vue
import { Component, Vue } from 'vue-property-decorator';

// import goes from:
// import CommonModule from '~/store/CommonModule';
// to:
import { commonModuleStore } from '~/store';

@Component
export default class Home extends Vue {
    get message(): string {
        // usage:
        commonModuleStore.SET_MESSAGE('hello world');
        console.log(commonModuleStore.message); // hello world
    }
}

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

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