【问题标题】:RxJs pipe and lettable operator `map`: 'this' context of type 'void' is not assignable to method's 'this' of type 'Observable<{}>'RxJs 管道和 lettable 运算符“map”:“void”类型的“this”上下文不可分配给“Observable<{}>”类型的方法“this”
【发布时间】:2018-04-22 06:16:48
【问题描述】:

我有一个非常基本的示例,它使用来自rxjs@5.5 的可让运算符mappipe

import { map } from 'rxjs/operator/map';

let o = of(1, 2, 3, 4).pipe(
    map((v) => v * 2)
);

但它会产生错误Error:(34, 5) TS2684:The 'this' context of type 'void' is not assignable to method's 'this' of type 'Observable&lt;{}&gt;'.这里有什么问题?

【问题讨论】:

    标签: javascript typescript rxjs


    【解决方案1】:

    Lettable 实例操作符应该从rxjs/operators 导入:

    import { map } from 'rxjs/operators';
    

    与从rxjs/operator 导入的不可出租等价物相反:

    import { map } from 'rxjs/operator/map';
    

    要了解有关 lettable 运算符的更多信息,请阅读:

    【讨论】:

    • 对于其他可能在谷歌上搜索的人:first 也是一个可出租的运算符
    • 令人沮丧的是 VSCode 似乎选择从 rxjs/operator/map 自动导入。
    • 有一些捆绑问题可能需要您对可管道操作符进行深度导入以减小捆绑大小。在这些情况下,如果您不小心从 rxjs/operator/map 而不是 rxjs/operators/map 导入,您将遇到此问题。
    • 谢谢大家,很难发现
    • 所有演示中必须清楚地显示导入内容 =/
    猜你喜欢
    • 2022-06-12
    • 1970-01-01
    • 2020-11-12
    • 2020-04-17
    • 2021-09-23
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    相关资源
    最近更新 更多