英文:
this.store.select(selectValues) Vs this.store.pipe(select(selectValues)) in NgRx
问题
- this.store.select(selectValues)
- this.store.pipe(select(selectValues))
- this.store.pipe(map(state => selectValues(state)))
英文:
On this page of NgRx selectors, there are few ways the select
function can be called.
- this.store.select(selectValues)
- this.store.pipe(select(selectValues))
- this.store.pipe(map(state => selectValues(state)))
What are the differences between the above three?
答案1
得分: 4
首先,this.store.select()
和 this.store.pipe(select())
本质上是相同的。
基于这个 文件,我们可以看到 Store
继承了 Observable
。此外,当调用 store.select()
时,我们可以看到调用了一个不同的 select
函数。这个函数被定义为一个 自定义的 RxJS 操作符,其返回类型是另一个函数,接受一个 Observable(即源 Observable)作为其唯一参数,也返回一个 Observable。
这意味着无论你选择哪种方法,都会得到相同的结果,两种方法都会返回一个你可以操作的 Observable。
与第三种方法的区别是,.map(state => selectValues(state))
,与前两种方法严格比较,这种方法不会在最后添加一个 distinctUntilChanged()
操作符。
英文:
First of all, this.store.select()
, and this.store.pipe(select())
are essentially the same.
Based on this file, we can see that Store
extends Observable
. Moreover, when store.select()
is called, we can see that a different select
function is called. This function is defined as a custom RxJS operator, which is a function whose return type is another function which accepts an Observable(i.e. the source Observable) as its sole argument and also returns an Observable.
This means that whichever approach you choose, the same result is achieved and both methods will return an Observable that you can work with.
The difference with the third approach, .map(state => selectValues(state))
, when strictly compared to the previous two, is that this one won't add a distinctUntilChanged()
operator at the end.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论