rxjs subcribe已弃用

发布于 2025-01-27 21:59:34 字数 335 浏览 6 评论 0原文

我在subcribe方法方面遇到了麻烦。在VSCODE中,它说sudcribe被弃用了,但我不知道如何正确更改它。

  public getAccount(): void{
    this.accountService.getAccounts().subscribe(
      (response: Account[]) => {
        this.accounts = response;
      },
      (error: HttpErrorResponse) => {
        alert(error.message);
      }
    )
  }

I have trouble with subcribe method. In vscode it says that subcribe is deprecated but I have no clue how to change it properly.

  public getAccount(): void{
    this.accountService.getAccounts().subscribe(
      (response: Account[]) => {
        this.accounts = response;
      },
      (error: HttpErrorResponse) => {
        alert(error.message);
      }
    )
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰ゝ天使的微笑 2025-02-03 21:59:34

您应该通过观​​察者对象而不是多个回调对象。所有使用多个参数的签名均已弃用。

this.accountService.getAccounts().subscribe({
  next: (response: Account[]) => {
    this.accounts = response;
  },
  error: (error: HttpErrorResponse) => {
    alert(error.message);
  },
  complete: () => {
    // do something when the observable completes
  }
});

如果您不需要错误并完成回调,则仍然可以这样使用:.subscribe((value)=> console.log(value))

您可以阅读有关您使用的签名为何被弃用

You should pass an observer object instead of multiple callbacks. All signatures that used multiple arguments were deprecated.

this.accountService.getAccounts().subscribe({
  next: (response: Account[]) => {
    this.accounts = response;
  },
  error: (error: HttpErrorResponse) => {
    alert(error.message);
  },
  complete: () => {
    // do something when the observable completes
  }
});

If you don't need an error and complete callbacks, you can still use it like this: .subscribe((value) => console.log(value)).

You can read about why the signature you're using was deprecated here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文