不建议使用订阅:使用观察者而不是错误回调 [英] Subscribe is deprecated: Use an observer instead of an error callback

查看:701
本文介绍了不建议使用订阅:使用观察者而不是错误回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行linter时,它会说:

When I run the linter it says:

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

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

Code (from an angular 7 app with angular-cli):

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

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

Don't know exactly what should I use and how...

谢谢!

推荐答案

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

subscribe isn't deprecated, only the variant you're using is deprecated. In the future, subscribe will only take one argument: either the next handler (a function) or an observer object.

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

So in your case you should use:

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

查看以下GitHub问题:

See these GitHub issues:

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

这篇关于不建议使用订阅:使用观察者而不是错误回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆