在Angular 2中,如果在subscribe函数中的else部分没有执行,可能是由于以下几个原因:
this.myService.getData().subscribe(
data => {
// 执行if部分
},
error => {
// 处理错误
},
() => {
// 执行else部分
}
);
async myFunction() {
const result = await this.myService.getData().toPromise();
if (result) {
// 执行if部分
} else {
// 执行else部分
}
}
this.myService.getData().subscribe(
data => {
if (data) {
// 执行if部分
} else {
console.log("数据为空");
// 执行else部分
}
}
);
通过检查这些可能的原因,您应该能够解决为什么else部分不会执行的问题。