gpt4 book ai didi

javascript - RxJS 5.5 错误 : take is not a function. 链式函数问题

转载 作者:行者123 更新时间:2023-11-29 20:59:24 26 4
gpt4 key购买 nike

我正在阅读关于 lettable/pipeable functions in RxJS 的介绍,但它没有说明这可能是链式方法的重大变化。这让我陷入了我们的一个 scipts,它在升级后开始抛出错误:

let things = new BehaviorSubject<Thing[]>([]);
things.filter((things) => things.length > 0).take(1).subscribe( ... )

ERROR TypeError: tasks.filter(...).take is not a function

有人可以解释/指出一篇解释为什么会发生这种情况的文章吗?顺便说一句,修复方法是将 take(1) 包装在 pipe() 函数中:

things.filter((things) => things.length > 0).pipe(take(1)).subscribe( ... )

..但我不确定为什么突然被迫这样做。

最佳答案

Angular 5 支持 RxJS 5.5,它引入了 lettable operators 应该有助于 tree-shaking 过程以在应用程序捆绑期间删除未使用的代码。您可以从 'rxjs/operators' 导入运算符,然后将它们包装到一个函数 pipe() 中,该函数采用可变数量的参数,例如:

import { debounceTime, map} from 'rxjs/operators';
...
let keyupValue$ = keyup$
.pipe(
debounceTime(500),
map(event => event['target'].value))
.subscribe(val => this.doSomething(val));

我更喜欢旧语法。我不认为这是一个突破性的变化。如果您不使用 import from 'rxjs/operators',您可以继续使用旧语法。

关于javascript - RxJS 5.5 错误 : take is not a function. 链式函数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47355760/

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