我想尝试通过使用在我的组件中进行比较如果成功显示消息否则在3秒内redirectToPage。但是,如果该*(this.link $ .source.value.success)或(this.link $ .getValue())*发送NULL,该如何检查。我尝试在控制台浏览器中显示this.link $,它显示的是BehaivorSubject。我尝试使用方法getValue()无法正常工作。您能帮我吗?
再次感谢
浏览器console.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 ???????
}
value
吸气剂是value
的一部分,而不是相应的可观察物。因此,您需要分配BehaviorSubject()
而不是可观察的值才能使用它。尝试以下]]
BehaviorSubject()
您只需要subscribe