为什么我应该在 Angular 订阅中将 select 与管道一起使用?

Why should I use select with pipe in Angular subscribe?

我正在阅读 angular 的文档及其对 RxJS 库的使用。 我找到了这个信息

Pipes

You can use pipes to link operators together. Pipes let you combine multiple functions into a single function. The pipe() function takes as its arguments the functions you want to combine, and returns a new function that, when executed, runs the composed functions in sequence.

所以管道的目的是链接多个函数,但是让我好奇的是我看到很多次使用pipe里面只有一个函数,例如:

this.itemSubscription = this.store
            .pipe(select(state => state.items.root))
            .subscribe(state => {
                this.items = state.items;
            });

当我尝试在没有 pipe 的情况下使用 select 时,我的 tslint 说:

select is deprecated: from 6.1.0. Use the pipeable select operator instead. (deprecation)tslint(1)

为什么会这样?我错过了什么吗?在网上找不到相关的解释。

使用可出租运营商被认为是最佳做法。因为您只能将您需要的特定运算符导入到您的项目中。其余的可以通过 tree shaking 移除,因此 bundle 的大小正在减小。

一开始只用一个操作符来使用管道似乎有点奇怪,但是你会习惯的。

Pipe 在 v5.5 中被引入 RxJS 采取看起来像这样的代码:

of(1,2,3).map(x => x + 1).filter(x => x > 2);

然后变成这个

of(1,2,3).pipe(
  map(x => x + 1),
  filter(x => x > 2)
);

相同的输出,相同的概念,但语法不同。

它清理了 'observable.prototype' 并使 RxJS 库更易于 tree-shakeable,您只需要导入您使用的内容。它还使编写和使用第三方运算符变得更加容易。

你在混东西。

select is deprecated: from 6.1.0. Use the pipeable select operator instead. (deprecation)tslint(1)

是指在pipe之外使用select。

this.store.select( ... )

pipe 中使用 select 是可行的方法。

import { Store, select } from '@ngrx/store';

this.store.pipe(select( ... ))

完全没问题。