i am creating a login form using Angular.when i login the system it will login successfully when it is redirect to another page it need to shown up the home page details but it will shown up both login and home page details.what i tried so far i attached below.
email: string = '';
password: string = '';
isLogin: boolean = true;
erroMessage: string = "";
constructor(private router: Router,private http: HttpClient) {}
login() {
console.log(this.email);
console.log(this.password);
let bodyData = {
email: this.email,
password: this.password,
};
this.http.post("http://localhost:9002/user/login", bodyData).subscribe( (resultData: any) => {
console.log(resultData);
if (resultData.status)
{
alert("Sucess");
this.router.navigate(['/home']);
this.isLogin = false;
}
else
{
alert("Incorrect Email or Password");
console.log("Errror login");
}
});
}
}
Routes
const routes: Routes = [
{
path: 'login',
component: LoginComponent
},
{
path: 'home',
component: HomeComponent,
}
];
LoginComponent
<form>
<div >
<label for="exampleInputEmail1">Email address</label>
<input type="text" [(ngModel)]="email" [ngModelOptions]="{standalone: true}" id="stname" placeholder="Enter Email"/>
</div>
<div >
<label for="exampleInputPassword1">Password</label>
<input type="password" [(ngModel)]="password" [ngModelOptions]="{standalone: true}" id="stname" placeholder="Enter Password"/>
</div>
<button type="submit" (click)="login()">Login</button>
</form>
CodePudding user response:
try defining a base path four your routing like :
{ path: '', component: HomepageComponent }