不建议使用订阅:使用观察者而不是错误回调

ism*_*tro 45 callback deprecated subscribe rxjs tslint

当我运行lint时,它说:

subscribe is deprecated: Use an observer instead of an error callback
Run Code Online (Sandbox Code Playgroud)

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

subscribe is deprecated: Use an observer instead of an error callback
Run Code Online (Sandbox Code Playgroud)

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

谢谢!

mar*_*tin 64

subscribe不建议弃用,仅弃用您使用的变体。将来,subscribe将仅采用一个参数:next处理程序(函数)或观察者对象。

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

.subscribe({
   next: this.handleUpdateResponse.bind(this),
   error: this.handleError.bind(this)
});
Run Code Online (Sandbox Code Playgroud)

请参阅以下GitHub问题:

  • idk...悬停在 vs code 中仍然显示该语法已弃用(rxjs 6.5.3) (52认同)
  • 嘿@YannicHamann [此评论](https://github.com/ReactiveX/rxjs/issues/4159#issuecomment-466630791)解释了原因。它并没有被弃用,他们只是弃用了其中一个重载,现在看起来一切都被弃用了。这主要是一个工具问题。 (17认同)
  • 我认为这个答案不再有效,因为所有订阅方法现在在 rxjs 6.5.4 中均已弃用 (4认同)
  • 是否有一个迁移脚本会自动更新我们所有的“subscribe()”方法?我们的项目中有数百个,我无法想象必须手动执行此操作! (4认同)
  • @AlokRajasukumaran 现在我们应该如何订阅? (4认同)
  • @AlokRajasukumaran *并非*所有方法现在都已弃用。只是某些工具会误解源代码并认为所有工具都已弃用。(参见:[github上的代码](https://github.com/ReactiveX/rxjs/blob/5e95503828798326947fc1dc1b98f69e9e04058c/src/internal/Observable.ts#L71)) (2认同)

Par*_*osh 50

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

VSCode 状态栏

TypeScript 版本选择器

选择本地 TypeScript 版本

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

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


小智 33

详情请见官网 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') 
})
Run Code Online (Sandbox Code Playgroud)


小智 19

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

之前的版本:

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

}, error => {
})
Run Code Online (Sandbox Code Playgroud)

新版本:

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

    error: (err: any) => { },
    complete: () => { }
  }
);
Run Code Online (Sandbox Code Playgroud)


Sim*_*ver 18

如果您将对象键入为Observable<T> | Observable<T2>- 而不是Observable<T|T2>.

例如:

    const obs = (new Date().getTime() % 2 == 0) ? of(123) : of('ABC');
Run Code Online (Sandbox Code Playgroud)

编译器并没有做出obsObservable<number | string>

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

obs.subscribe(value => {

});
Run Code Online (Sandbox Code Playgroud)

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

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


mag*_*ker 15

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

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

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