Tom*_* N. 1 javascript vue.js axios
我正在尝试使其能够从第一个axios调用中查看返回的对象,如果为空,请继续执行第二个操作(如果不为空,我将制作一条错误消息)
基本上,仅当userStatus对象为空时,才应发生第二次axios调用。两个axios调用都是独立工作的,但是如果对象为空,我如何才能正确地执行此工作,以便打第二个调用?
目前,我在第一个axios调用上得到200,在控制台中有一个空的userStatus对象,但是第二个调用没有发生
changeStatus: function(event) {
let user = this.auth_user;
axios.get('/user/' + user + '/status')
.then((response) => {
this.userStatus = response.data
})
if(this.userStatus.length < 1){
let data = {
id: event.id,
status: 'A'
};
axios.post('/status/change',data)
.then((response) => {
if (response.data.success == false) {
this.errors = [];
const errorLog = Object.entries(response.data.errors);
for (var i = errorLog.length - 1; i >= 0; i--) {
console.log(errorLog[i][1][0]);
this.errors.push(errorLog[i][1][0]);
}
}
})
}else{
console.dir('No');
}
},
Run Code Online (Sandbox Code Playgroud)
问题是,正在执行的代码同步(基本上是行由行),而爱可信调用是一个同步的。因此,尽管您的第一个axios调用仍在后台执行,但if(this.userStatus.length < 1)在您的第一个调用返回之前,该语句将得到评估。
如果你的第二个电话是在你第一次调用的结果为条件,你需要让你的第二个电话里的.then()首次呼叫的处理程序:
changeStatus: function(event) {
let user = this.auth_user;
axios.get('/user/' + user + '/status')
.then((response) => {
this.userStatus = response.data;
if(this.userStatus.length < 1) {
let data = {
id: event.id,
status: 'A'
};
axios.post('/status/change',data)
.then((response) => {
if (response.data.success == false) {
this.errors = [];
const errorLog = Object.entries(response.data.errors);
for (var i = errorLog.length - 1; i >= 0; i--) {
console.log(errorLog[i][1][0]);
this.errors.push(errorLog[i][1][0]);
}
}
});
} else {
console.dir('No');
}
});
},
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
35 次 |
| 最近记录: |