简体   繁体   English

如何让我的 Angular BehaviorSubject 触发?

[英]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.

相关问题 Angular - 如何在不调用 getValue 的情况下更新 function 中的 BehaviorSubject 存储值中的字段 - Angular - how can I update a field in my BehaviorSubject store value in a function without calling getValue 在Angular2中修改Observable会导致行为引发火灾 - Modifying Observable in Angular2 Causes BehaviorSubject to Fire 如何将 BehaviorSubject 传递给 MatDialog 数据? - How can i pass BehaviorSubject to MatDialog data? 如何访问 Angular 中 BehaviorSubject 的属性? - How to access properties of BehaviorSubject in Angular? Angular fire Messaging 无法接收通知,我该如何解决? - Angular fire Messaging Not able to recieve Notificaation, How Can I Fix this? 如何使用@angular/fire 连接两个数据库? - How can I connect two databases with @angular/fire? 如何在不调用next的情况下设置新的BehaviorSubject值? - How can I set a new BehaviorSubject value without calling next? 如何获取 BehaviorSubject 中的值以在 getter 中返回? - How do I get a value in a BehaviorSubject to return within a getter? 我的 Angular 应用程序中 BehaviorSubject 的令人困惑的行为 - Confusing behavior of a BehaviorSubject in my Angular App 如何从服务中的其他服务订阅BehaviorSubject? - How can I subscribe to a BehaviorSubject from another service in a service?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM