In the Nuxt documentation ( here ) it says ‘You can optionally break down a module file into separate files: state.js
, actions.js
, mutations.js
and getters.js
。
我似乎找不到任何关于如何完成此操作的示例 - 许多将根级别的 Vuex 存储分解为 state.js
, actions.js
, mutations.js
和 getters.js
,并放入单独的模块文件中,但与分解模块本身无关。
所以目前我有:
├── assets
├── components
└── store
├── moduleOne.js
├── moduleTwo.js
└── etc...
我想要的是:
├── assets
├── components
└── store
└── moduleOne
└── state.js
└── getters.js
└── mutations.js
└── actions.js
└── moduleTwo
└── etc...
要尝试这个,在 /store/moduleOne/state.js
我有:
export const state = () => {
return {
test: 'test'
}
};
在 /store/moduleOne/getters.js
我有:
export const getters = {
getTest (state) {
return state.test;
}
}
在我的组件中,我正在使用 $store.getters['moduleOne/getters/getTest']
访问它
但是使用调试器和 Vue devtools,似乎无法在 getters 文件中访问状态 - 它似乎正在本地文件中寻找状态,因此 state.test
未定义。
尝试将 state
从我的 state.js
文件导入到我的 getters.js
文件似乎也不起作用。
有没有人举例说明他们如何在 Nuxt 中像这样分解商店?
原文由 Guy Harper 发布,翻译遵循 CC BY-SA 4.0 许可协议
我正在使用 nuxt
2.1.0
如果你想要这样的东西:在我的
store/index.js
确保你有 namespaced: true
在模块一
在我的
store/api-logic/index.js
在我的
store/api-logic/getters.js
在模块二
在我的
store/app-logic/index.js
在我的
store/app-logic/getters.js
应用程序中的任何位置
奖励积分
如果您想在模块之间进行通信,例如应用程序逻辑中的操作会触发 api 逻辑中的某些内容。所以app-logic(模块一)到api-logic(模块二)
当您指定
root: true
时,它将开始查看商店的根目录。在
store/app-logic/actions.js
在
store/api-logic/actions.js
在
store/api-logic/index.js
添加另一个条目在
store/api-logic/mutations.js
添加口袋妖怪突变 :p应用程序中的任何位置:
最后你的 Vue devTool 应该是这样的:)
瞧,我希望这很清楚。代码示例:
https://github.com/CMarzin/nuxt-vuex-modules