路由器导航在 Angular 中不起作用

时间:2021-03-11 15:52:17

标签: angular angular-routing angular-router

我在我的 angular 项目中设置了路由,下面是我在根模块中的路由

const appRoutes: Routes = [
  { path: '', component: LoginComponent },
  { path: 'dashboard', canActivate: [AuthGuard], component: DashboardComponent }
];

在我的 app.component.ts 中有我的 <router-outlet></router-outlet>

在我的登录页面中,我有一个功能可以在成功登录后导航到 http://localhost:4200/dashboard

登录.component.ts

login() {
....
this.router.navigate(['dashboard'], {relativeTo: this.route});
....
}

现在的问题是,当我在浏览器中执行 ng serve 并打开 localhost:4200 时,我将无法登录。只有在重新加载页面后,即 localhost:4200 我才能登录。

是什么阻止我在不重新加载的情况下登录?请让我知道:)

1 个答案:

答案 0 :(得分:1)

我认为当您尝试导航到路由“仪表板”时,路由守卫会拒绝您的操作,因为可能不是有效的令牌或 AuthGuard 中的某些逻辑。您必须检查您的 AuthGuard