angular - 将多个 ngrx 存储选择合并为单个调用 Angular 7

标签 angular typescript rxjs

我基本上需要给我的 ngrx 打 3 个电话店铺

所以我所拥有的是

getUserInfo() {
    this._store.select('userInfo')
       .subscribe(result => this.userInfo = result);
}

getCats() {
    this._store.select('cats')
       .subscribe(result => this.cats = result);
}

getDogs() {
    this._store.select('dogs')
        .subscribe(result => this.dogs = result);
}

现在我试图将其浓缩为一个方法,所以我尝试了这个

我正在像这样导入 rxjs
import { combineLatest } from 'rxjs';
import { tap, filter } from 'rxjs/operators';

这是我的方法
getStoreData() {
    combineLatest(
        this._store.select('userInfo'),
        this._store.select('cats'),
        this._store.select('dogs')
    ).pipe(tap(([userInfo, cats, dogs]) => console.log(userInfo, cats, dogs));
}

我像这样调用我的方法
ngOninit() {
   this.getStoreData()
}

我的问题是正在调用该方法,但我从未获得控制台日志?

我不确定我做错了什么

编辑

我也试过
getStoreData {
    forkJoin(
      this._store.pipe(select('userInfo')),
      this._store.pipe(select('xberts')),
      this._store.pipe(select('tasks'))
    ).subscribe(res => console.log(res));
}

但还是同样的问题,没有 console.log()
任何帮助,将不胜感激!

最佳答案

我认为你需要订阅。试试这个:

getStoreData() {
    combineLatest(
        this._store.select('userInfo'),
        this._store.select('cats'),
        this._store.select('dogs')
    ).pipe(filter(e => !e.includes(null)),tap(([userInfo, cats, dogs]) => console.log(userInfo, cats, dogs))
    .subscribe()
}

或者
ngOninit() {
   this.getStoreData().subscribe()
}

关于angular - 将多个 ngrx 存储选择合并为单个调用 Angular 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55527943/

相关文章:

Angular 2 指令的生命周期 Hook (ngAfterContentInit) 无法按照文档中的描述工作

javascript - 使用 TypeScript 的 RxJ : How generate Production files for Web?

angularjs - Typescript 类装饰器扩展方法

html - 从对象数组中打印一个对象属性并在 angular5 中执行上一个和下一个

javascript - RxJS:将历史数据与更新流相结合

rxjs - 将新的 rxjs observable 合并到现有订阅的 observable 中

java - 如何解决 Angular 12 中的 CORS 问题?

javascript - 如何在 Protractor 测试中检查应用程序 URL 的状态代码?

java - 仅当我选择一个选项时, Angular 显示按钮和区域

typescript - 类型 Window 上不存在 XMLHttpRequest