【问题标题】:"subscribe()" is deprecated when using "of(false)"使用“of(false)”时不推荐使用“subscribe()”
【发布时间】:2021-03-12 23:12:43
【问题描述】:

我正在尝试使用of,但我的编辑说它已被弃用。如何使用of 使其正常工作?

  public save(): Observable<ISaveResult> | Observable<boolean> {
    if (this.item) {
      return this.databaseService.save(this.userId, this.item)
    }
    return of(false)
  }
  public componentSave() {
    const sub = this.userService
      .save()
      .subscribe(val => {
        // This callback shows the below error in the editor
      })
  }

当我使用它时,我的编辑器中出现以下错误:

@deprecated — Use an observer instead of a complete callback

'(next: null, error: (error: any) => void, complete: () => void): Subscription' is deprecated
Expected 2-3 arguments, but got 1.

【问题讨论】:

标签: javascript angular typescript rxjs


【解决方案1】:

没关系,我认为我找到了问题所在。从

更改保存函数的返回类型
public save(): Observable<ISaveResult> | Observable<boolean>

public save(): Observable<ISaveResult | boolean>

【讨论】:

  • 我实际上没有使用返回类型。我只是在问题中使用它。它实际上是从返回值推断出来的。
  • 那么我回到我原来的评论。您需要提供一个包含实际损坏代码的示例。
  • 如果我不推断返回,那么它可以正常工作,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-15
  • 2022-08-03
  • 2016-04-22
  • 2012-11-14
  • 1970-01-01
  • 2019-11-08
  • 2019-11-08
相关资源
最近更新 更多