You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular5+中takeWhile is not a function问题求助

问题原因与解决办法

嘿,这个问题我太熟了!你遇到的是RxJS版本升级带来的操作符调用方式变化的问题。

为什么会报错?

从RxJS 6.x版本开始,所有的管道操作符(比如takeWhile、map、filter)都不能直接通过Observable链式调用了,必须要通过.pipe()方法来包裹使用。你现在的代码还是用了RxJS 5.x及更早版本的写法,所以才会抛出takeWhile is not a function的错误。

解决办法

只需要把takeWhile放到pipe()方法里就行,修改后的代码如下:

public ngOnInit(){ 
  this.schemeStore.getSchemesByAffId()
    .pipe(
      takeWhile(() => this.isAlive)
    )
    .subscribe((schemes: Scheme[]) => { 
      const currId = schemes[0].currency; 
      this.schemeFunds$ = this.fundStore
        .getSchemeValorizationsByCurrency(currId); 
    }); 
}

额外提醒

如果你后续还要使用其他RxJS操作符(比如map、switchMap、filter等),都要遵循这个规则:把所有操作符都放进pipe()方法中,用逗号分隔多个操作符。

内容的提问来源于stack exchange,提问作者Florian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:55:58