Angular 2的Array Subject .next()不会调用其他组件的观察者

Angular 2 Array Subject .next() not invoking another component's observer

本文关键字:调用 其他 组件 观察者 Array Subject next Angular      更新时间:2023-09-26

我使用firebase作为我的后端。

在data.service中。最后,我创建了一个Subject数组,它将由应用程序init上的firebase观察者填充:

private orders = new Subject<any>();
orders$ = this.orders.asObservable();
firebase.database().ref(this.fbDataPath).on('child_added', (childSnapshot) => {
  this.orders.next(
    {
      key: childSnapshot.key,
      name: childSnapshot.val().name,
      items: childSnapshot.val().items
    }
  )
})

然后我用DataService提供一个单独的目录组件,并订阅它的订单观察:

DataService.orders$.subscribe(
  order => {
    console.log('subscribe hit')
  })

我似乎无法让侦听器组件在next上触发。我为布尔isLoggedIn做了这个工作,在这个场景中我一定错过了一些东西。谢谢!

这可能是因为您在闭包中使用了this。从this.orders.next()中去除this