【发布时间】:2018-02-01 14:09:35
【问题描述】:
我的操作有以下设置:
get1: ({commit}) => {
//things
this.get2(); //this is my question!
},
get2: ({commit}) => {
//things
},
我希望能够从另一个动作中调用一个动作,所以在这个例子中我希望能够从get1() 中调用get2()。这可能吗?如果可以,我该怎么做?
【问题讨论】:
我的操作有以下设置:
get1: ({commit}) => {
//things
this.get2(); //this is my question!
},
get2: ({commit}) => {
//things
},
我希望能够从另一个动作中调用一个动作,所以在这个例子中我希望能够从get1() 中调用get2()。这可能吗?如果可以,我该怎么做?
【问题讨论】:
你可以访问第一个参数传入的对象中的dispatch方法:
get1: ({ commit, dispatch }) => {
dispatch('get2');
},
documentation 对此进行了介绍。
【讨论】:
then()?
dispatch('B/someaction')
您可以通过第一个参数(上下文)访问调度方法:
export const actions = {
get({ commit, dispatch }) {
dispatch('action2')
}
}
但是,如果您使用命名空间,则需要指定一个选项:
export const actions = {
get({ commit, dispatch }) {
dispatch('action2', {}, { root: true })
}
}
【讨论】:
对于不需要有效负载的操作
actions: {
BEFORE: async (context, payload) => {
},
AFTER: async (context, payload) => {
await context.dispatch('BEFORE');
}
}
对于确实需要有效负载
的操作actions: {
BEFORE: async (context, payload) => {
},
AFTER: async (context, payload) => {
var payload = {}//prepare payload
await context.dispatch('BEFORE', payload);
}
}
【讨论】:
我们也可以在调度时传递参数。
dispatch('fetchContacts', user.uid);
【讨论】:
export actions = {
GET_DATA (context) {
// do stuff
context.dispatch('GET_MORE_DATA');
},
GET_MORE_DATA (context) {
// do more stuff
}
}
【讨论】: