wumi_blog icon indicating copy to clipboard operation
wumi_blog copied to clipboard

vuex action 中调用 action

Open 5Mi opened this issue 8 years ago • 0 comments

当从一个模块中调用另一个模块的 action 时,或者调用同一模块中的另一个 action 时,切记,action 的第一个参数是 store 实例,因此应该将调用者 action 的第一个参数传递给被调用 action。 如果你使用 ES6 的解构形式来编写 action,确保调用者 action 的第一个参数包含两个 action 中用到的所有属性和方法。举例说明,调用者 action 仅使用 dispatch 方法,而被调用 action 使用了 state 属性和 watch 方法,那么,dispatch、state 和 watch 应该都出现在传递给调用者 action 的第一个形式参数中,示例如下:

import {callee} from './anotherActionModule'

export const caller = ({dispatch, state, watch}) => {
  dispatch('MUTATION_1')
  callee({state, watch})
}
import {reqLocationinfo} from '../../api/api.js';
import {setProductInfo} from '../../../common/vuex/actions.js';


export async function initProductInfo({dispatch,state},id){
    //产品信息 
    await setProductInfo({dispatch,state},id);
    let addressId = state.productInfo.addressId;
    console.log(addressId)
    //地址信息
    let locationinfo = await reqLocationinfo(addressId);
    if(locationinfo){
        dispatch('INIT_LOCATION',locationinfo);
    }
}

5Mi avatar Jun 17 '16 14:06 5Mi