【问题标题】:VSCode auto import - Not working for Rxjs operatorsVSCode 自动导入 - 不适用于 Rxjs 运算符
【发布时间】:2023-02-16 16:03:00
【问题描述】:

我有一个项目,其中 VS Code 在使用 rxjs 运算符时不会建议导入。

myObservable.pipe(
    take(1),
    filter(result => result),
    switchMap((result) => myOtherObserbable),
    finalize(() => this.isLoading = false)
)

在这种情况下,takefilterswitchMapfinalize 不会在上下文菜单中提供任何建议:

我必须手动写: import { take, filter, switchMap, finalize } from "rxjs/operators";

然后它起作用了。

是什么原因导致的,我应该在哪里寻找?

【问题讨论】:

    标签: visual-studio-code rxjs


    【解决方案1】:

    tsconfig 文件中发现问题:

    "paths": {
        "@app/*": ["app/*"],
        "@env/*": ["environments/*"],
        "@angular/*": ["../node_modules/@angular/*"],
        "rxjs/*": ["../node_modules/rxjs/*"], // => Commenting this makes the imports work
        "@custom/library": ["../../../path/to/custom/library/src"]
    }
    

    我正在使用自定义库,必须为项目添加路径才能知道在哪里查找,但显然它破坏了自动导入

    【讨论】:

      【解决方案2】:

      tsconfig.json请添加:

      {
        "compilerOptions": {
          "typeRoots": ["node_modules/@types"]
        }
      }
      

      By default all visible ”@types” packages are included in your compilation.

      这样不仅可以找到rxjs/operators,还可以找到其他库类型。

      【讨论】:

        猜你喜欢
        • 2018-08-20
        • 2016-12-02
        • 1970-01-01
        • 2020-12-17
        • 2022-10-19
        • 2018-07-06
        • 1970-01-01
        • 2018-04-26
        • 2023-04-10
        相关资源
        最近更新 更多