setUsersData() 和 addNewUser() 完成其工作后返回 Observable。 即使这部分代码工作正常,来自 tap 运算符的函数也不会执行,如果我将逻辑移动到订阅,情况也是如此。有什么想法为什么会发生吗?
this.userDataService.setUsersData().pipe(
take(1),
switchMap(() => {
if (name !== undefined) {
console.log('New User created');
return this.userDataService.addNewUser(resData.email, resData.localId, name);
} else {
console.log('Name is undefined. Skipping user creation.');
return of();
}
}),
tap(() => {
console.log('data setted');
this.isLoading = false;
this.router.navigate(['/microblog']);
})
).subscribe();
我尝试切换 RxJs 运算符,但没有任何改善。
我的函数 addNewUser() 和名称为 undefined 的 case 都是 return of(),正如我刚刚了解到的那样,它并没有真正创建一个 Observable。我更改了上面的内容以返回 of(void 0) 并且它工作得很好。