【发布时间】:2020-04-29 18:36:57
【问题描述】:
我想尝试通过使用在我的组件中进行比较 如果(成功)显示消息 否则在 3 秒内重定向到页面。 但是,如果它发送 NULL,我如何检查这个 *(this.link$.source.value.success) 或 (this.link$.getValue()) *。我尝试在我的控制台浏览器中显示 this.link$,它显示的是 BehaivorSubject。我尝试使用 getValue() 方法,但它不起作用。你能帮帮我吗?
再次感谢
浏览器控制台.log
Service.ts
private objectRes: BehaviorSubject<ResModel>;
public activationOrResetResponse: Observable<ResModel>
constructor(
public _apiService: ApiService,
) {
super(_apiService);
this.objectRes = new BehaviorSubject(null) as BehaviorSubject<ResModel>;
this.activationOrResetResponse = this.objectRes.asObservable();
}
public activateOrResetAccount(token: string, typeRoute: string): void {
this._apiService.get(this.type + "/" + typeRoute + "/" + token).subscribe(
(res) => {
this.activationOrReset = res;
this.objectRes.next(this.activationOrReset);
},
(error) => {
this.activationOrReset = error;
this.objectRes.next(this.activationOrReset);
}
);
}
Component.ts
public link$: Observable<ResModel>;
constructor(
private _authUserService: AuthUserService,
private _activeRoute: ActivatedRoute,
) {}
ngOnInit(): void {
this.getActivationPage();
}
public getActivationPage(): void {
this.link$ = this._authUserService.activationOrResetResponse;
this._authUserService.activateOrResetAccount(
this._activeRoute.snapshot.params.token,
"confirmation"
);
console.log("link$: ", this.link$.getValue()); // null ???????
}
【问题讨论】:
标签: angular rxjs behaviorsubject