In Angular-13, I am implementing reactive form. I have this code in the component:
createForm() {
this.loginForm = new FormGroup({
userName: new FormControl('', Validators.required),
password: new FormControl('', [Validators.required, Validators.minLength(6), Validators.maxLength(80)])
})
}
login(){
this.authService.login(this.loginForm.value).subscribe(res=>{
this.router.navigateByUrl('/');
}, error=>{
console.log(error);
this.toastr.error(error.error);
})
}
But I got this error and subscribe is crossed:
The declaration was marked as deprecated
How do I get it resolve?
Thanks
CodePudding user response:
Pass an observer as argument.
const observer = {
next: x => console.log('Observer got a next value: ' x),
error: err => console.error('Observer got an error: ' err),
complete: () => console.log('Observer got a complete notification'),
};
In your case since you only need to react to next and error notifications, it would be like this.
login(){
this.authService.login(this.loginForm.value).subscribe({
next: (res) => this.router.navigateByUrl('/'),
error: (error) => {
console.log(error);
this.toastr.error(error.error);
}
})
}
cheers
