[英]Commit mutation in a different Vuex module
I got the following structure:我得到以下结构:
store.js
|
|-- yields.js
|
|--analysis (sub-folder)
|
|
---actions.js
|
---mutations.js
|
---state.js
In mutations.js I set the date of a state in state.js like:在 mutation.js 中,我在 state.js 中设置了 state 的日期,例如:
stateFetchParamsStart: (state, input) => {
state.fetchParams.start = input;
// Want to change yields.PRdate too
//state.yields.PRdate = input;
}
How can I access the PRdate state of yields.js with the mutation from state.js?如何使用 state.js 的突变访问 yield.js 的 PRdate state?
EDIT with more information:编辑更多信息:
I'm setting initial values of PRdate
when the component is mounted like:当组件安装时,我正在设置PRdate
的初始值,如下所示:
mounted(){
...
this.fetchPRData(this.pvSystem.system_id); // makes an axios call
}
Also I've set a computed property asking for the date:我还设置了一个计算属性来询问日期:
getPrDate: {
get(){
return this.$store.state.yields.PRdate;
}
}
Now when clicking on a date picker I want the date to change (also computed propery on a v-model
):现在,当单击日期选择器时,我希望更改日期(也在v-model
上计算属性):
start: {
get() {
return this.$store.state.analysis.fetchParams.start
},
set(value) {
this.$store.commit('stateFetchParamsStart', value)
}
}
You can change a Vuex module's state from another module, but it has to be done through an action .您可以从另一个模块更改 Vuex 模块的 state,但必须通过action来完成。 That action can call a mutation in the other module.该动作可以调用另一个模块中的突变。 So first, create a mutation in your yields module:所以首先,在你的产量模块中创建一个突变:
mutations: {
statePRdate(state, input) {
state.PRdate = input;
}
}
Now you can use an action in the analysis module to commit that mutation.现在您可以使用分析模块中的操作来提交该突变。 Pass a third argument to the commit
call:将第三个参数传递给commit
调用:
{ root: true }
The first argument will be the mutationName or moduleName/mutationName if your modules are namespaced.如果您的模块是命名空间的,第一个参数将是mutationName或moduleName/mutationName 。 Here is the new action below that calls both a mutation in its own module and a mutation in another module:这是下面的新操作,它同时调用自己模块中的突变和另一个模块中的突变:
actions: {
analysisAction({ commit }, input) {
commit('stateFetchParamsStart', input); // Commit in this module
commit('statePRdate', input, { root: true }); // Commit in another module
}
}
Initiate the action like:启动如下操作:
this.$store.dispatch('analysisAction', 'input')
If your modules are namespaced, then you'd use namespaced syntax for both the commit and the action:如果您的模块是命名空间的,那么您将对提交和操作使用命名空间语法:
commit('yields/statePRdate', input, { root: true })
this.$store.dispatch('analysis/analysisAction', 'input')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.