【问题标题】:error TS2339: Property 'startWith' does not exist on type 'Subject<string>'错误 TS2339:“主题<字符串>”类型上不存在属性“startWith”
【发布时间】:2019-08-05 08:30:16
【问题描述】:

我在 Angular 7 项目中遇到以下错误。我已经使用 npm 安装了 typescript,这可能意味着最新的 typescript 版本。

错误 TS2339:“主题”类型上不存在属性“startWith”。

app.component.ts

this.language
    .startWith(this.translationService.getBrowserLang())
    .subscribe(lang => this.store.dispatch(new LanguageAction(lang)))

tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  }
}

tsconfig.app.json

【问题讨论】:

标签: angular


【解决方案1】:

您试图以错误的方式使用。 您还必须在主题上使用pipe

https://www.learnrxjs.io/operators/combination/startwith.html

此外,当您使用主题时,它不再是字符串。 并且您正在尝试使用字符串的实用程序。

【讨论】:

    【解决方案2】:

    就像@The Mechanic 已经说过的那样,您正在尝试直接在Subject 上使用startWith 方法。从 5.5 版开始,运算符为 pipeable。如果要过滤掉正确的值,请改用:

    this.language.pipe(
      startWith(this.translationService.getBrowserLang())
    ).subscribe(lang => this.store.dispatch(new LanguageAction(lang)));
    

    【讨论】:

      猜你喜欢
      • 2016-03-29
      • 2018-08-27
      • 2021-06-22
      • 1970-01-01
      • 2019-01-21
      • 2016-08-13
      • 2019-10-23
      • 2017-12-20
      • 2016-11-14
      相关资源
      最近更新 更多