需求:
頁面加載后獲取接口A,然后獲取接口B
通過A接口獲取info,info為接口B的參數
A和B在vue不同的組件里,這就涉及到接口返回的一個時間差
假如A接口在A組件內,B接口在B組件內。
解決方案:
A組件請求A接口后存放在vuex
state: {
info:null
}
B組件內通過computed進行實時計算監聽
computed: {
info() {
return this.$store.state.info;
}
}
然后,通過watch監聽info,監聽到有值后,就可以調用接口B
needInfo(fn) {
let watchAch = this.$watch("info", (val, oldVal) => {
if (val) {
this[fn]();
watchAch(); //取消監聽
}
});
},
