置信大家在工作中肯定会遇到这种状况,在申请一个接口胜利当前在调用另一个接口,这个时候如果依照惯例的写法来写的话,在接口胜利当前的回调里写另一个接口的申请办法,这样就会呈现一种状况,第一个接口还没有返回胜利就会调用第二个接口,这是因为ajax是异步申请的起因,那么就须要用到es6的语法了,以下是实例心愿对大家有所帮忙
getList(){
axios.get('api/getData.php',{ // 还能够间接把参数拼接在url后边
params:{
title:'眼镜'
}
}).then(function(res){
this.getOtherList;
}).catch(function (error) {
console.log(error);
});
}
//业务
next(){
this.getOtherList().then(() =>{
this.getList()
})
}
getOtherList(){
return axios.get('api/getOtherData.php',{
params:{
title:'眼镜'
}
}).then(function(res){
console.log(res.data.data)
}).catch(function (error) {
console.log(error);
});
}
getOtherList办法调用接口胜利当前在调用getList的接口
发表回复