Reputation: 15
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.
Upvotes: 1
Views: 4797
Reputation: 747
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.
Upvotes: 2