【发布时间】:2019-10-02 10:25:35
【问题描述】:
我正在构建一个 Angular 7 应用程序并使用 BehaviorSubject 来保持用户身份验证状态,正如互联网上每个来源所推荐的那样。
既然 BehaviorSubject 是一个 Observable,为什么我不能触发 onComplete() 方法?
这是代码(我觉得这很经典):
this.authService.authenticationState.subscribe(state => {
this.isLoggedIn = state;
},
err => console.log(err),
() => console.log('complete')
);
身份验证服务
authenticationState = new BehaviorSubject(false);
“完成”未记录。是不是我做错了什么?
解决方案
this.authService.authenticationState.subscribe(state => {
this.isLoggedIn = state;
this.authService.authenticationState.complete();
},
err => console.log(err),
() => console.log('complete')
);
然后触发 complete() 方法
【问题讨论】:
标签: angular typescript rxjs behaviorsubject