在我的 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/