Angular - 如何在 ngOnInit() 中解析 .subscribe 的 "the declaration was marked as deprecated"

标签 angular

在 Angular-13 的 typescript 中,我得到了以下代码:

   ngOnInit(): void {
    this.appSettingsService.getSettings()
      .subscribe(settings => this.settings = settings,
      () => null,
      () => {
         this.defaultSidebarColor = this.settings.defaultSidebarColor;
         this.defaultBrandlogoColor =  this.settings.defaultBrandlogoColor;
      });
  }

但是出现了错误,如下所示:

"the declaration was marked as deprecated" for .subscribe

我的 .subscribe 被划掉了。

如何解决这个问题?

提前谢谢您。

最佳答案

这里有完整的详细信息和示例:https://rxjs.dev/deprecations/subscribe-arguments

总结一下:签名 subscribe(next,error,complete) 已被弃用,转而使用“next”、“error”和“complete”中的一个或多个键传递对象。

在您的示例中,您可以这样重构:

  .subscribe({
     next:settings => this.settings = settings,
     error:() => null,
     complete:() => {
        this.defaultSidebarColor = this.settings.defaultSidebarColor;
        this.defaultBrandlogoColor =  this.settings.defaultBrandlogoColor;
     }
  });

关于Angular - 如何在 ngOnInit() 中解析 .subscribe 的 "the declaration was marked as deprecated",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72717320/

相关文章:

Angular 2 RC4 到最新 - "Uncaught TypeError: Cannot read property ' prototype' of undefined"when extending

javascript - 登录后隐藏登录按钮,反之亦然

http - Angular 2 : Show activity indicator for every HTTP request and hide views until completed

javascript - 仅当对象的属性发生更改时,Angular 才更新 View

asp.net-mvc - .NET MVC 5 中的 Angular 6 应用程序, Angular 路由不起作用

angular - 使用仅获取一个对象的 AngularFire2 查询查询 Firebase

angular - Ionic 2/Angular 2 组件生命周期钩子(Hook)

angular - 为什么 MatDialog 组件中的 Angular Form 非常慢?

angular - 无法从 Gitlab 文件获取数据 - 出现 Cors 异常

javascript - debounceTime 使表单控件运行两次