棱角分明 – 儿童看守:canActivateChild无法正常工作
发布时间:2020-12-17 17:22:55 所属栏目:安全 来源:网络整理
导读:我试图像角度doc一样建立一个儿童看守: @Injectable()export class AuthGuardService implements CanActivate,CanActivateChild { constructor(private authService: AuthentificationService,private router: Router) {} canActivate(route: ActivatedRout
|
我试图像角度doc一样建立一个儿童看守:
@Injectable()
export class AuthGuardService implements CanActivate,CanActivateChild {
constructor(private authService: AuthentificationService,private router: Router) {}
canActivate(route: ActivatedRouteSnapshot,state: RouterStateSnapshot): boolean {
let url: string = state.url;
return this.checkLogin(url);
}
canActivateChild(route: ActivatedRouteSnapshot,state: RouterStateSnapshot): boolean {
return this.canActivate(route,state);
}
checkLogin(url: string): boolean {
/*****/
return false;
}
}
我的routing.module: import { AuthGuardService } from '../services/auth-guard.service';
const routes = [
{
path: '',component: MyComponent,canActivate: [AuthGuardService],children: [{
path: '',canActivateChild: [AuthGuardService],children: [
{
path: 'candidature',component: ListCandidatureComponent,},{
path: 'candidature/new',component: NewCandidatureComponent
}]
},{
path: 'login',component: LoginComponent,}]
}
]
我将canActivateChild保护放在无组件部分上,以通过身份验证来保护此路由. 但是使用这种配置,当我试图达到’my.site.com/candidature’时,我遇到了这个错误: Unhandled Promise rejection: guard is not a function ; Zone: angular ; Task: Promise.then ; Value: TypeError: guard is not a function 通常,如果我没有被认证,我需要被重定向到登录页面. 谢谢 解决方法
canActivateChild:[AuthGuardService]不应该在子节点内,而是在父节点上声明.
不确定,当你在孩子里面宣布孩子时,你需要在外面的孩子中宣布canActivateChild.但你可以测试有没有它.如果有效,请告诉我! const routes = [
{
path: '',canActivateChild: [AuthGuardService] // it should be placed here!
children: [{
path: '',// canActivateChild: [AuthGuardService],// needed or not?
children: [
{
path: 'candidature',}]
}
]
希望这可以帮助! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- unix – 如何使sed从脚本文件读取脚本?
- 超详细vim操作解析
- 连接数百个RxScala Observable(每个都有数百万个要发出的事
- 简单的Websocket在scala/akka/play
- 使用docker和fig设置mongodb副本集的正确方法是什么?
- angularjs – 在Mean.js堆栈上找不到Angular.js指令templat
- angularjs – 如何在角度js中设置默认时区
- angularjs – Angular UI-Grid(新的) – 基于行的动态/自动
- 在if语句bash脚本中使用和(\u0026\u0026)运算符
- 解决webService+axis2生成的wsdl文件有两个Bindings/Endpoi
