在Rxjs中使用管道时可观察到不订阅-Angular

时间:2019-05-23 20:22:37

标签: angular rxjs

以下代码订阅:

this.store
      .select(petSelectors.selectPetData)
      .pipe(find(x => x.petName === petName)).subscribe(x => console.log(x));

但是,如果我这样做,它会确实订阅:

this.store.select(petSelectors.selectPetData).subscribe(x => console.log(x));

更新:这也可以

const x = this.store .select(petSelectors.selectPetData).pipe(find(x => x));

但是当我添加逻辑时,它不会

我正在关注RxJS官方文档: https://rxjs-dev.firebaseapp.com/api/operators/find

我什至必须在pipe(), try with take(1), map(), etc内尝试,当我执行.subcribe()时,什么也没打印。我也尝试过使用async管道。

对象:

{
  "petName": "devpato"
}

我要传递的宠物名称是将对象与“ devpato”进行比较

1 个答案:

答案 0 :(得分:0)

发现了我的问题! @ConnorsFan基本上x => x.petName === petName不起作用,因为x是整个数组,而不是数组内部的单个对象。所以我需要贯穿x

.pipe(map(x => x.find(u => u.petName ===petName));