gpt4 book ai didi

angular - 为什么我应该在 Angular 订阅中使用 select with pipe?

转载 作者:行者123 更新时间:2023-12-05 09:12:12 25 4
gpt4 key购买 nike

我正在阅读 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)

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

最佳答案

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 库更易于摇树优化,您只需要导入您使用的内容即可。它还使编写和使用第三方运算符变得更加容易。

关于angular - 为什么我应该在 Angular 订阅中使用 select with pipe?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58872125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com