问题是:当this.getPost()))返回错误,例如404(返回reter get的函数),然后未执行代码this.loggedChange.emit(false)
,我不知道为什么。在这种情况下,我的loggedchangedhandler输出错误。看起来像内部错误,在swtichmap之后,this.loggedChange没有"观察"。在SwitchMap之前,它已经观察到了,所以我认为它可以是线索,但是我不知道为什么会这样起作用。如果第一个函数返回错误(this.authlogin-它也恢复get),则this.loggedChange.emit工作正常。
儿童组件:
login(): void {
this.authLogin(this.email, this.password)
.pipe(
tap(() => this.loggedChange.emit(true)),
switchMap(() => this.getPost())
)
.subscribe(
resp => {
//some code here
},
error => {
this.loggedChange.emit(false);
}
);
}
在父组件中,我有类似的东西:
<login-component (loggedChange)="loggedChangeHandler($event)"></login-component>
和ts
loggedIn$ = new BehaviorSubject(false);
loggedChangedHandler(el: boolean): any {
this.loggedIn$.next(el);
}
我认为您有这种行为,因为错误发生在您的this.getPost()
呼叫中。
由于您没有错误处理程序,但可观察到的尝试仍试图继续进行错误。
您可以尝试在管道中添加catchError
:
this.authLogin(this.email, this.password)
.pipe(
tap(() => this.loggedChange.emit(true)),
switchMap(() => this.getPost()),
catchError(error => this.loggedChange.emit(false))
)
.subscribe(
resp => {
//some code here
},
error => {
this.loggedChange.emit(false);
}
);