Nuxt.js 自定义存储文件夹

Nuxt.js custom store folder

我尝试更改 nuxt.js 默认使用的 vuex 存储路径。我希望我的商店路径是 'modules/my-module/store/store.js'。有什么办法可以做到这一点?或者我可以通过某种方式将我的模块存储从 nuxt 模块文件添加到现有存储中?

是的,您可以使用 registerModule 函数动态注册商店模块。

假设您有 my-module 模块,里面有 index.vue 文件。在该文件中,您可以像这样注册模块的商店:

import store from './store'; //import your module store
export default {
  name: 'my-module',
  computed: {
    ...
  },
  created() {
    this.$store.registerModule('myModuleStore', store);
  },
  mounted() {
    this.$store.dispatch('myModuleStore/someAction'); //example of action for your module's store
  },
};

我向您推荐这篇 medium 文章,它提出了一些很好的 vue.js 应用程序结构,包括存储模块,其中显示了如何注册私有模块