Aitsuken
Aitsuken

Reputation: 126

RxJS subcribe is deprecated

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

Upvotes: 1

Views: 666

Answers (1)

Tsvetan Ganev
Tsvetan Ganev

Reputation: 8856

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.

Upvotes: 4

Related Questions