-1
this.userService.addUser(this.user).subscribe(
  (data)=>{
    //success
    console.log(data);
    this._snack.open('Registered Successfully', 'OK', {
      duration: 2000,
      verticalPosition: 'top',
      horizontalPosition: 'center',
      panelClass: ['green-snackbar', 'login-snackbar'],
    });
  },
  (error)=>{
    //error
    console.log(error);
    this._snack.open('Something went Wrong', 'OK', {
      duration: 2000,
      verticalPosition: 'top',
      horizontalPosition: 'center',
      panelClass: ['red-snackbar','login-snackbar'],
    });
  }
);

我不知道到底发生了什么,但订阅显示已弃用,以下是消息

(method) Observable<Object>.subscribe(next?: ((value: Object) => void) | null | undefined, error?: ((error: any) => void) | null | undefined, complete?: (() => void) | null | undefined): Subscription (+2 overloads)
@deprecated — Instead of passing separate callback arguments, use an observer argument. Signatures taking separate callback arguments will be removed in v8. Details: https://rxjs.dev/deprecations/subscribe-arguments

'(next?: ((value: Object) => void) | null | undefined, error?: ((error: any) => void) | null | undefined, complete?: (() => void) | null | undefined): Subscription' is deprecated.ts(6385)
Observable.d.ts(55, 9): The declaration was marked as deprecated here.

顺便说一句,我正在使用 Angular13

4

1 回答 1

3

只需将其更改为:

this.userService.addUser(this.user).subscribe({
  next: (data) => {
    console.log(data);
    this._snack.open('Registered Successfully', 'OK', {
      duration: 2000,
      verticalPosition: 'top',
      horizontalPosition: 'center',
      panelClass: ['green-snackbar', 'login-snackbar'],
    });
  },
  error: (error) => {
    console.log(error);
    this._snack.open('Something went Wrong', 'OK', {
      duration: 2000,
      verticalPosition: 'top',
      horizontalPosition: 'center',
      panelClass: ['red-snackbar','login-snackbar'],
    });
  }
});
于 2021-11-30T06:29:39.070 回答