Both pages are Shown up Angular

Advertisements

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 class="form-group">
      <label for="exampleInputEmail1">Email address</label>
      <input type="text" [(ngModel)]="email" [ngModelOptions]="{standalone: true}" class="form-control" id="stname" placeholder="Enter Email"/>
    </div>
    <div class="form-group">
      <label for="exampleInputPassword1">Password</label>
      <input type="password" [(ngModel)]="password" [ngModelOptions]="{standalone: true}" class="form-control" id="stname" placeholder="Enter Password"/>
    </div>
    <button type="submit" class="btn btn-primary" (click)="login()">Login</button>
  </form>

>Solution :

try defining a base path four your routing like :

{ path: '', component: HomepageComponent }

Leave a Reply Cancel reply