CanActivate导致节流历史状态错误

时间:2018-11-23 15:47:04

标签: angular

每当我导航到/about页时,我的应用都会陷入无限循环,并显示错误Throttling history state changes to prevent the browser from hanging

每当我导航到/页面时,我的应用程序都不会导航到dashboard,它只会显示空白页面。

我希望我的页面在找到令牌时导航到/dashboard,在找不到令牌时导航到/home/

此外,如果找到了令牌,我希望下面链接的其余页面无法访问(除了仪表板)。

我该如何解决?

app.routing.component.ts

const routes: Routes = [
  { path: 'about', component: AboutComponent, canActivate: [AuthGuard] },
  { path: 'home', component: HomeComponent },
  { path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard] },
  { path: 'careers', component: CareersComponent },
  { path: 'contact', component: ContactComponent },
  { path: 'legal', component: LegalComponent },
  { path: 'login', component: LoginComponent },
  { path: 'press', component: PressComponent },
  { path: 'markets', component: MarketsComponent },
  { path: 'markets/:symbol', component: PriceComponent },
  { path: 'register', component: RegisterComponent },
  { path: 'status', component: StatusComponent },
  { path: 'services', component: ServicesComponent },
  { path: 'support', component: SupportComponent },
  { path: 'support/account-management', component: AccountManagementComponent },
  { path: 'support/transactions', component: TransactionsComponent },
  { path: 'support/payment-methods', component: PaymentMethodsComponent },
  { path: 'support/security', component: SecurityComponent },
  { path: 'support/task-administration', component: TaskAdministrationComponent },
  { path: 'support/wallet-services', component: WalletServicesComponent },
  { path: '**', redirectTo: '', pathMatch: 'full' },
  { path: '', component: HomeComponent },
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule],
})
export class AppRoutingModule implements OnInit {
  constructor() { }

  ngOnInit() {
    RouterModule.forRoot(routes, { scrollPositionRestoration: 'enabled' });
  }
}

auth.guard.ts

@Injectable()
export class AuthGuard implements CanActivate {
  constructor(private authService: AuthService,
    private router: Router) {
  }

  canActivate(
    _next: ActivatedRouteSnapshot,
    _state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
    if (this.authService.isLoggedIn()) {
      this.router.navigate(['dashboard']);
      if (environment.production === false) {
        console.log('Token found, redirecting to dashboard');
      }
      return true;
    } else {
      this.router.navigate(['home']);
      if (environment.production === false) {
        console.log('Token not found, redirecting to home');
      }
      return false;
    }
  }
}

auth.service.ts

@Injectable()
export class AuthService {
  constructor(private myRoute: Router) { }

  getToken() {
    return localStorage.getItem('token');
  }

  isLoggedIn() {
    return this.getToken() !== null;
  }

  logout() {
    localStorage.removeItem('token');
    this.myRoute.navigate(['login']);
  }
}

1 个答案:

答案 0 :(得分:1)

首先,我认为您需要将通配符放在最后:

  { path: '', component: HomeComponent },
  { path: '**', redirectTo: '', pathMatch: 'full' }

第二,如果您希望根据令牌无法访问页面,则应在这些页面上添加防护。

第三,您已经将/映射到HomeComponent,因此它将永远不会重定向到DashboardComponent

第四,我相信无限循环是因为您的AuthGuard重定向到/dashboard。该路径由AuthGuard保护,该路径重定向到/dashboard,依此类推。依此类推。您不应重定向到由进行重定向的同一保护程序保护的路径。

我认为您应该做的是将您的警卫分为两部分:

  • /home的一个守护程序,如果有令牌,则可以重定向到仪表板。如果没有令牌,该警卫应该重定向到首页,因为这已经是当前路径!
  • /dashboard的一个守护者,以及在没有令牌的情况下重定向到home的其他路径。如果有令牌,该警卫应该重定向到仪表板!