221

当我运行 linter 时,它说:

subscribe is deprecated: Use an observer instead of an error callback

代码(来自带有 angular-cli 的 Angular 7 应用程序):

    this.userService.updateUser(data).pipe(
       tap(() => {bla bla bla})
    ).subscribe(
       this.handleUpdateResponse.bind(this),
       this.handleError.bind(this)
    );

不知道我应该使用什么以及如何使用...

谢谢!

4

8 回答 8

294

subscribe不推荐使用,仅不推荐使用您使用的变体。将来,subscribe将只接受一个参数:next处理程序(函数)或观察者对象。

因此,在您的情况下,您应该使用:

.subscribe({
   next: this.handleUpdateResponse.bind(this),
   error: this.handleError.bind(this)
});

请参阅这些 GitHub 问题:

于 2019-04-02T10:19:12.817 回答
86

可能有趣的是,observer对象还可以(仍然)包含complete()方法和其他附加属性。例子:

.subscribe({
    complete: () => { ... }, // completeHandler
    error: () => { ... },    // errorHandler 
    next: () => { ... },     // nextHandler
    someOtherProperty: 42
});

这样,省略某些方法就容易多了。对于旧签名,有必要提供undefined并坚持论证的顺序。现在,例如仅提供下一个和完整的处理程序时,情况就更加清楚了。

于 2019-07-11T09:19:11.087 回答
54

对我来说,这只是我的 VSCode 指向的打字稿版本。

VSCode 状态栏

TypeScript 版本选择器

选择本地 TypeScript 版本

从这个GitHub 评论中获得帮助。

我相信这是一个打字稿问题。最新版本的打字稿中的某些内容导致此警告显示在 vs 代码中。通过单击 vs code 右下角的 typescript 版本,然后选择 select typescript version 选项,我可以让它消失。我将它设置为我们在 Angular 项目中安装的 node_modules 版本,在我们的例子中恰好是 4.0.7。这导致警告消失。

于 2021-03-18T13:07:31.743 回答
21

如果您的对象类型为Observable<T> | Observable<T2>- 而不是Observable<T|T2>.

例如:

    const obs = (new Date().getTime() % 2 == 0) ? of(123) : of('ABC');

编译器使用obstype Observable<number | string>

您可能会惊讶于以下内容会给您错误Use an observer instead of a complete callbackExpected 2-3 arguments, but got 1.

obs.subscribe(value => {

});

这是因为它可以是两种不同类型之一,而编译器不够聪明,无法协调它们。

您需要将代码更改为 returnObservable<number | string>而不是Observable<number> | Observable<string>. 这其中的微妙之处会因你在做什么而有所不同。

于 2019-07-11T04:35:18.770 回答
3

我将我的Angular项目从TSLintto迁移到ESLint,现在它不再显示警告了!

我遵循了这些步骤。(每个步骤结束我也建议提交更改)

  1. 添加 eslint: ng add @angular-eslint/schematics

  2. 将 tslint 转换为 eslint: ng g @angular-eslint/schematics:convert-tslint-to-eslint

  3. 删除tslintcodelyzernpm uninstall -S tslint codelyzer

  4. 如果您想自动修复许多 Lint 问题 ng lint --fix(它还会列出未修复的问题)

  5. 在 VSCode 中卸载TSLint插件,安装ESLint插件并重新加载 VSCode。

  6. 确保它更新了包和包锁定文件。还有你项目中的 node_modules。

  7. 如果您tsconfig.json在子目录下有文件 - 您需要使用文件projects-root-directory/.vscode/settings.json所在的子目录添加/更新tsconfig

    {
      "eslint.workingDirectories": [
        "sub-directory-where-tsconfig-files-are"
      ]
    }
    
于 2021-05-05T21:25:59.980 回答
2

我收到警告是因为我通过这个来订阅:

myObs.subscribe(() => someFunction());

由于它返回单个值,因此它与subscribe's 函数签名不兼容。

切换到此使警告消失(返回 null/void);

myObs.subscribe(() => {
  someFunction();
});
于 2020-05-22T17:15:30.857 回答
1

你应该用 eslint 替换 tslint。

由于 TSLint 已被弃用,它不支持@deprecatedRXJS 的语法。ESLint 是正确使用的 linter,可以正确订阅 linting。

于 2021-04-04T13:15:10.197 回答
0

在官方网站上找到详细信息 https://rxjs.dev/deprecations/subscribe-arguments

请注意{}下面第二个订阅代码中的大括号。

import { of } from 'rxjs';

// recommended 
of([1,2,3]).subscribe((v) => console.info(v));
// also recommended
of([1,2,3]).subscribe({
    next: (v) => console.log(v),
    error: (e) => console.error(e),
    complete: () => console.info('complete') 
})
于 2022-02-13T09:13:45.540 回答