为什么我应该在 Angular 订阅中使用带有管道的 select ?

Isl*_*aev 2 select store pipe rxjs angular

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

管道

您可以使用管道将运算符连接在一起。管道允许您将多个函数组合成一个函数。pipeline() 函数将要组合的函数作为参数,并返回一个新函数,该函数在执行时会按顺序运行组合函数。

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

this.itemSubscription = this.store
            .pipe(select(state => state.items.root))
            .subscribe(state => {
                this.items = state.items;
            });
Run Code Online (Sandbox Code Playgroud)

当我尝试使用selectwithoutpipe时,我的 tslint 会说:

select 已弃用:从 6.1.0 开始。请改用可管道选择运算符。(弃用)tslint(1)

为什么会发生这种情况?我错过了什么吗?在网上找不到相关解释。

Jay*_*yme 5

Pipe在 v5.5 中被引入 RxJS,以获取如下所示的代码:

of(1,2,3).map(x => x + 1).filter(x => x > 2);
Run Code Online (Sandbox Code Playgroud)

并将其变成这样

of(1,2,3).pipe(
  map(x => x + 1),
  filter(x => x > 2)
);
Run Code Online (Sandbox Code Playgroud)

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

它清理了“observable.prototype”并使 RxJS 库更加可摇树,您只需要导入您使用的内容。它还使得编写和使用第三方运算符变得更加容易。