每当我导航到/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']);
}
}发布于 2019-03-19 20:56:02
首先,我认为您需要将通配符放在最后:
{ path: '', component: HomeComponent },
{ path: '**', redirectTo: '', pathMatch: 'full' }第二,如果您想要一个页面很难访问取决于一个令牌,那么您应该在这些页面上添加一个保护。
第三,您已经将/映射到HomeComponent,因此永远不会重定向到DashboardComponent。
第四,我相信无限循环是因为您的AuthGuard重定向到/dashboard。该路径由AuthGuard保护,该路径重定向到/dashboard等。您不应该重定向到由与重定向相同的保护所保护的路径。
我认为你应该做的是把你的警卫分成两半:
/home就可以重定向到仪表板。如果没有令牌,这个守卫不应该重定向到家里,因为这已经是当前的路径了!/dashboard和其他重定向路径的守卫。如果有令牌,这个守卫不应该重定向到仪表板!https://stackoverflow.com/questions/53449570
复制相似问题