async changeSelectPosition (item: any) { console.log(this.formEntity.DrillOrgIDarr, 'this.formEntity.DrillOrgIDarr') let checkResult if(this.formEntity.ExamType==0){ checkResult = this.checktypetask(this.formEntity.ExamType, this.formEntity.DrillOrgIDarr, item.value[0].ID) }else{ checkResult = this.checktypetask(this.formEntity.ExamType, this.formEntity.ByCheckOrgIDs, item.value[0].ID) } if(checkResult){ this.formEntity.PostName = item.value[0].text this.formEntity.PostId = item.value[0].ID this.showPosition = false }else{ return } },我已经这么做了,他还是会进行if里的赋值操作
时间: 2024-02-10 20:17:11 浏览: 116
原因是 `checktypetask` 函数是异步函数,执行完异步操作后才返回结果,而在 `changeSelectPosition` 函数中,没有等待 `checktypetask` 函数执行完毕,就直接进行了赋值操作,因此无法正确处理返回值。
你可以在 `changeSelectPosition` 函数中加上 `await`,等待 `checktypetask` 函数执行完毕并返回结果后,再进行赋值操作,例如:
```javascript
async changeSelectPosition(item: any) {
console.log(this.formEntity.DrillOrgIDarr, 'this.formEntity.DrillOrgIDarr');
let checkResult;
if (this.formEntity.ExamType == 0) {
checkResult = await this.checktypetask(this.formEntity.ExamType, this.formEntity.DrillOrgIDarr, item.value[0].ID);
} else {
checkResult = await this.checktypetask(this.formEntity.ExamType, this.formEntity.ByCheckOrgIDs, item.value[0].ID);
}
if (checkResult) {
this.formEntity.PostName = item.value[0].text;
this.formEntity.PostId = item.value[0].ID;
this.showPosition = false;
} else {
return;
}
}
```
这样,当 `checktypetask` 函数返回 `false` 时,就会直接跳过赋值操作。
阅读全文