Recently, I came across this login method in Angular:
email: string = "";
password: string = "";
login() {
const credentials = {
email: this.email,
password: this.password
};
this.userService.login(credentials).subscribe(
(response: any) => {
sessionStorage.setItem('token', response.token);
sessionStorage.setItem('email', response.email)
this.router.navigate(['/dashboard']);
},
(error) => {
console.error('Login failed:', error);
}
);
this.modalService.dismissAll();
}
This method is part of my own userService.
login(data: any): Observable<any> {
return this.httpClient.post(this.url + "/login", data, {
headers: new HttpHeaders().set('Content-Type', 'application/json')
});
}
However, every time I declare the userService in the main page constructor and implement the logic, my modal stops working.
constructor(private userService:UserService,private modalService:NgbModal,
private router:Router) {
}
I attempted consolidating the methods into one file to troubleshoot, but it still didn't resolve the issue. I am expecting the modal to open as intended, but it remains non-functional.