简体   繁体   中英

Typescript Deprecation warning

I am using this code for login but subscribe shows depricated. I am new to typescript, so can anyone help me out

doLogin() 
{
  
     this.userService.doLogin(this.loginForm.value).subscribe(
     result => 
     {
     console.log(result);
     localStorage.setItem('userData', JSON.stringify(result));
     this.router.navigate(['/list-user']);
     this.toastr.success('Success', 'Logged In Successfully');
     },
     (error) => 
     {
     console.log(error);
     this.toastr.error('Failed', 'Invalid Credentials');
     });
}

this works in an other project I've worked on but shows depricated here and I'm getting this error:

@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.

As per the link

import { of } from 'rxjs';
// recommended 
of([1,2,3]).subscribe((v) =>         
console.info(v));
// also recommended
of([1,2,3]).subscribe({
    next: (v) => console.log(v),
    error: (e) => console.error(e),
    complete: () =>     
        console.info('complete') 
})

You will no longer need to pass more than one callback. If needed you can pass in an observable object containing the callbacks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM