[英]How can I get my Angular BehaviorSubject to fire?
I have the following set up, but it doesn't work.我有以下设置,但它不起作用。 I never see the alert popup.
我从来没有看到警报弹出窗口。
// this is in ngOnInit
this.getBackDate$.subscribe();
//this.$spService.clientProfile$ is as follows:
public clientProfile$: Observable<ClientProfile | null> = this.client$.pipe(
tap(()=> this.clientProfileIsLoading$.next(true)),
switchMap( (client) => {
if (!client) {
return of<ClientProfile>(null);
}
return this.$http.get<ClientProfile>(ApiDomain.supervisionPlan, `/v1/Client/ClientProfile?clientIdToken=${client.entityIdToken}`)
}),
tap(() => this.clientProfileIsLoading$.next(false))
);
...
getSelectedClient$ = new BehaviorSubject<ClientProfile>(null);
getBackDate$ = this.getSelectedClient$.pipe(
withLatestFrom(this.$spService.clientProfile$),
tap(([client]) => {
alert(client.caseStart)
this.maxBackdate = client.caseStart;
})
);
EDIT:编辑:
If I change to this it works, but I don't want to use combineLatest, as I am not combining anything.如果我改变它,它可以工作,但我不想使用 combineLatest,因为我没有组合任何东西。
getBackDate$ = combineLatest([this.$spService.clientProfile$]).pipe(
tap(([client]) => {
alert(client.caseStart)
this.maxBackdate = client.caseStart;
})
);
If you have a look here you can see that both observables need to emit a value.如果您在这里查看,您会发现两个可观察对象都需要发出一个值。 Otherwise nothing will happen.
否则什么都不会发生。 So you could do the following:
因此,您可以执行以下操作:
clientProfileReplay$ = this.$spService.clientProfile$.pipe(
shareReplay({ bufferSize: 1, refCount: true })
);
And then use clientProfileReplay$ in withLatestFrom().然后在 withLatestFrom() 中使用 clientProfileReplay$。
Seems like an asynchronous bug you have got there看起来像你有一个异步错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.