当我打开缰绳时,上面写着:

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

angular应用的代码:

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

我不知道该用什么,怎么用……

谢谢!


当前回答

详见官方网站 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') 
})

其他回答

使用RxJS的新方法非常简单:

以前的版本:

this.activatedRoute.queryParams.subscribe(queryParams => {
console.log("queryParams, queryParams)

}, error => {
})

新版本:

  this.activatedRoute.queryParams.subscribe(
  {
    next: (queryParams) => {
      console.log('queryParams', queryParams);
    },

    error: (err: any) => { },
    complete: () => { }
  }
);

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

我遵循这些步骤。(在每个步骤结束时,我还建议提交更改)

Add eslint: ng add @angular-eslint/schematics Convert tslint to eslint: ng g @angular-eslint/schematics:convert-tslint-to-eslint Remove tslint and codelyzer: npm uninstall -S tslint codelyzer If you like to auto fix many of the Lint issues ng lint --fix (It will also list the not fixed issues) In VSCode uninstall the TSLint plugin, install ESLint plugin and Reload the VSCode. Make sure it updated the package and package-lock files. Also the node_modules in your project. If you have the tsconfig.json files under sub directory - you need to add/update the projects-root-directory/.vscode/settings.json with the sub directory where the tsconfig files are! { "eslint.workingDirectories": [ "sub-directory-where-tsconfig-files-are" ] }

VS Code官方页面的信息:从TSLint迁移到ESLint(谢谢你在评论中指出这一点!) Angular从TSLint迁移到ESLint Reference

我得到这个警告是因为我正在传递这个来订阅:

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

由于它返回单个值,因此与订阅的函数签名不兼容。

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

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

详见官方网站 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') 
})

也许有趣的是,观察者对象也可以(仍然)包含complete()方法和其他附加属性。例子:

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

这样可以更容易地省略某些方法。对于旧的签名,必须提供未定义的并坚持参数的顺序。现在,只提供下一个完整的处理程序就清楚多了。