angular - ngrx - 链接两个 store.select 切片

标签 angular rxjs ngrx

在我的 Todo Cmp 中我有这段代码

this.todoListGroup$ = this.ngrx.select(fromRoot.getTodos)
    .flatMap((todos: Todo[]) => {
        console.log(todos)
        this.todos = todos
        this.ngrx.select(fromRoot.getLastChangedTodo);
    })
    .map(lastTodo =>{{
        console.log(lastTodo)
        doSomething(this.todos, lastTodo)
    })

当我订阅它时,每次待办事项更改时我都会收到一个 console.log(lastTodo)。我认为使用 flatmap 和 ngrx.select,我每次都订阅了一个新的 Observable?

我可以用哪个操作符链接两个存储切片?

编辑:

只要 View 在 DOM 中,我就想继续订阅 todoListGroup$,因为它应该不断更新我的 View 。

到目前为止,我的解决方案是在 reducer 中定义一个新的切片,它返回两个所需的属性。但是,我仍然对哪个运算符可以有效地链接 ngrx 单个属性切片感兴趣。

谢谢!

最佳答案

这是 RxJs v6+ 的一种方法,使用 combineLatest运营商。

首先像这样导入运算符和Observable函数:

import { Observable, combineLatest } from 'rxjs';
import { tap } from 'rxjs/operators';

然后你可以像这样使用它们:

combineLatest([
  this.store.pipe(select('users', 'userList')),
  this.store.pipe(select('users', 'accountsList')),
]).pipe(tap(([userList, accountsList]) => console.log(userList, accountsList)))

关于angular - ngrx - 链接两个 store.select 切片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47268772/

相关文章:

javascript - 返回值后多次调用 ngrx 效果

angular - NGRX 8, Angular ,如何处理未定义的错误

angular ngrx/store 何时以及如何加载数据

redux - Electron 不能添加redux开发工具,未捕获的异常: SyntaxError: Octal literals are not allowed in strict mode

javascript - 找不到模块 : Error: Can't resolve 'fs' in

Angular Material 2 DatePicker,动态改变显示格式

Angular 6 : error TS2315: Type 'ModuleWithProviders' is not generic

javascript - 如何在 Angular 2 中迭代这个 json 响应

typescript - RXJS 控制可观察调用

angular - ngrx 文档引用解析