当我在“收获前”子路径中使用RoleGuard并打开浏览器时,浏览器完全被阻止,似乎处于无限循环中。我没有任何编译问题,也无法打开控制台查看我有什么错误。
可以在子路径中使用 canActivate 吗?还是我应该使用 CanActivateChild?CanActivateChild 没有这个问题。
const preHarvestRoutes: Routes = [
{
path: '',
component: PrivateComponent,
canActivate: [AuthGuard],
children: [
{
path: 'pre-harvest',
component: PreHarvestComponent,
canActivate: [RoleGuard], <------- IF I REMOVE THIS I DO NOT HAVE ANY PROBLEM.
children: [
{
path: 'new-field',
component: NewFieldComponent
},
]
}
]
}
];
角色守卫:
import { Injectable } from '@angular/core';
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { UserRolesService } from '../services/user-roles.service';
import { webStorage } from "../utils/web-storage";
@Injectable()
export class RoleGuard implements CanActivate {
constructor(
private userRoles: UserRolesService
, private router: Router
) { }
canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot ) {
//let roles = route.data['roles'] as Array<string>;
//let rolesUserLogged = webStorage.user;
this.router.navigate( ['pre-harvest'] );
return true;
}
}