需求:在实际项目的开发应用中我们需要先执行前两个方法,用两个方法返回的数据作为参数去执行第三个方法
这时候我们就需要使用new promise、resolve、promise.all等方法
- 使用代码如下所示:可根据实际情况进行变通
- 方法1
// 账户信息获取
getAccount(){
return new Promise((resolve,reject)=>{
this.$axios.post(CONFIG.httphost() + '/user/queryUser',{
st:1,
limit:0
}).then(resp =>{
let res = resp.data;
let code = res.code;
if(!code){
this.accountList = res.data;
//值得注意的是这里,new promise的方法正确请求是要resolve出来,才能继续向下执行
resolve();
}
})
})
},
- 方法2
// 获取单位
getDW() {
return new Promise((resolve,reject)=>{
this.$axios.post(CONFIG.httphost() + "/MessageShowController/showDwMessage", {
st: 1,
limit: 0
}).then((resp) => {
let res = resp.data;
let code = res.code;
if (!code) {
this.dwList = res.data;
resolve()
} else {
this.giveMsg("请求数据失败");
}
});
})
},
- 方法3
// 获取数据
getData() {
Promise.all([
this.getAccount(),
this.getDW(),
]).then(res=>{
//此处的请求方法根据实际封装变通
this.$axios.post(CONFIG.httphost() + "/MessageCollectorController/showBmJurisdiction", {
dw_id: self.dw_id,
userid:this.userid
})
.then((resp) => {
let res = resp.data;
let code = res.code;
if (!code) {
this.$nextTick(() => {
this.getRoleMenu(res.data);
});
}
})
.catch((err) => {
});
})
},