【问题标题】:Both pages are Shown up Angular两个页面都显示为 Angular
【发布时间】:2023-01-25 20:09:20
【问题描述】:

我正在使用 Angular 创建一个登录表单。当我登录系统时,它将在重定向到另一个页面时成功登录,它需要显示主页详细信息,但它会同时显示登录和主页详细信息。我试过了我附在下面。

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");
        }
      });
    }
}

航线

常量路线:路线= [

  {
    path: 'login',
    component: LoginComponent
  },
  {
  path: 'home',
  component: HomeComponent,
  
  }


];

【问题讨论】:

  • 请包括完整的代码示例来演示错误

标签: angular


【解决方案1】:

使用this.router.navigateByUrl('/home')而不是this.router.navigate(['/home']);

【讨论】:

  • 我试过还是一样
  • 你定义了你的基本路径吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-16
  • 2019-02-11
  • 1970-01-01
相关资源
最近更新 更多