有一个大组件。里面包含四个子组件,我在第一个组件中请求接口获取了参数 a 和 b ,由于后面三个组件请求的有些接口都用到了参数 a 和 b ,在第一个组件请求的接口成功后我把 a 和 b 保存到看 state 里面,但是事实上后面三个组件在请求接口时 a 和 b 的值还是为空,请问有什么好的解决方案?
第一个组件触发的 action :
export default{ getCityInfo({commit,state}){ return fetch('GET','/v1/cities',{type:'guess'}).then(msg =>{commit(mutation_types.GET_POSITION,msg);}); } } mutations :
export default{ [mutation_types.GET_POSITION](state,msg){ state.a = msg.a; state.b = msg.b; } } 因为在组件一还有其他的操作: getCityInfo.then(其他操作).then();
然后在组件二中需要 a 和 b ,所以我又要这样: getCityInfo.then(其他操作).then()
感觉很重复很冗余
备注:组件请求接口都是在 mounted 时期发生的
