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

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)
    );

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

谢谢!


当前回答

对我来说,这只是我的VSCode指向的typescript版本。

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

我相信这是打字稿的问题。在最新版本的typescript中,某些东西导致这个警告显示在vs code中。我可以通过点击vs code右下角的typescript版本,然后选择select typescript版本选项来让它消失。我将它设置为我们在angular项目中安装的node_modules版本,在我们的例子中恰好是4.0.7。这使得警告消失了。

其他回答

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

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

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

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

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

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

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

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

订阅不被弃用,只是您使用的变体被弃用。以后,subscribe将只接受一个参数:要么是下一个处理程序(函数),要么是一个观察者对象。

所以在你的情况下,你应该使用:

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

请看这些GitHub问题:

https://github.com/ReactiveX/rxjs/pull/4202 https://github.com/ReactiveX/rxjs/issues/4159

对我来说,这只是我的VSCode指向的typescript版本。

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

我相信这是打字稿的问题。在最新版本的typescript中,某些东西导致这个警告显示在vs code中。我可以通过点击vs code右下角的typescript版本,然后选择select typescript版本选项来让它消失。我将它设置为我们在angular项目中安装的node_modules版本,在我们的例子中恰好是4.0.7。这使得警告消失了。

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