首页 > 解决方案 > 如果用户使用navigateByUrl更改密码不起作用,Angular 6重定向到应用程序的启动组件

问题描述

我目前在我的 Angular 6 应用程序的以下网址:

http://localhost:4200/dashboard/admin/users

如果管理员更改了他的凭据,我需要再次将他重定向到登录组件。

this.auth.updateCredentials(credentials).subscribe(
  (data) => {
    if (data == "success") {
      this.showError = false;
      if (credentials['user_id'] == this.loggedUser) {
        //Redirect
        localStorage.setItem('jwt', '')
        localStorage.setItem('user_id', '')
        localStorage.setItem('user_role', '')
        this.router.navigateByUrl('login');
      } else {
        this.dialogRef.close();
      }
    }
    if (data == "error") {
      this.showError = true;
    }

  },
  (error) => {
    this.showError = true;
    console.log(error);
  }
);

我试过了this.router.navigateByUrl('login');this.router.navigateByUrl('/login');

但没有错误,也没有重定向

标签: angularangular6router

解决方案


尝试这个:

constructor(..., private router: Router, ...) {}
...
this.router.navigate(['/login']);
...

推荐阅读