我无法弄清楚我失踪的地点和地点。我正在尝试为子路径创建授权。如果用户是 - >仅限管理员,他可以查看网址,如果用户是---> notAdmin,然后只有他不能查看某些网址。我已经做了一个授权,在login.component.ts,我检查用户是否是Admin并将其存储在localStorage中,以便进一步访问,但它不起作用,当我分开放入网址并访问页面时。
EG。我无法授权访问此特定网址 - > notAdmin
http://localhost:4200/#/master-system/add-group
我得到的错误是 -
Error: Cannot match any routes. URL Segment: 'master-system/update-system'
P.S:更新用户组件是否必须强制进入主用户组件? 代码
路由-module.ts
const routes: Routes = [
{
path: '',
data: {
title: 'Master System'
},
children: [
{
path: 'master-systems',
component: MasterSystemsComponent,
canActivateChild:[AuthGuard],
data: {
title: 'Master Systems'
},
children:[
{
path: 'add-system',
component: AddSystemComponent,
data: {
title: 'Add'
}
},
{
path: 'update-system',
component: UpdateSystemComponent,
data: {
title: 'Update'
}
}
]
},
{
path: 'master-interface',
component: MasterInterfaceComponent,
canActivateChild:[AuthGuard],
data: {
title: 'Master Interface'
},
children:[{
path: 'add-interface',
component: AddInterfaceComponent,
data: {
title: 'Add'
}
},
{
path: 'update-interface',
component: UpdateInterfaceComponent,
data: {
title: 'Update'
}
}]
},
{
path: 'master-user',
component: MasterUserComponent,
canActivateChild:[AuthGuard],
data: {
title: 'Master User'
},
children:[
{
path: 'add-user',
component: AddUserComponent,
data: {
title: 'Add'
}
},
{
path: 'update-user',
component: UpdateUserComponent,
data: {
title: 'Update',
expectedRole: '18'
},
},
]
},
{
path: 'master-group',
component: GroupComponent,
canActivateChild:[AuthGuard],
data: {
title: 'Master Group'
},
children:[
{
path: 'add-group',
component: AddGroupComponent,
data: {
title: 'Add',
expectedRole: '18'
}
},
{
path: 'update-group',
component: UpdateGroupComponent,
data: {
title: 'Update'
}
},
]
},
{
path: 'master-role',
component: RoleComponent,
canActivateChild:[AuthGuard],
data: {
title: 'Master Role'
},
children:[ {
path: 'add-role',
component: AddRoleComponent,
data: {
title: 'Add'
}
},
{
path: 'update-role',
component: UpdateRoleComponent,
data: {
title: 'Update'
}
},
]
}, ]
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class MasterSystemRoutingModule { }
authGuard.ts
import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot,
CanActivateChild } from '@angular/router';
@Injectable()
export class AuthGuard implements CanActivateChild, CanActivate {
expectedRole: any;
accessId:boolean=false;
constructor(private router: Router) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot)
{
if (localStorage.getItem('CurrentUser')){
console.log("IN AUTHGUARD", localStorage.getItem('CurrentUser'));
console.log("inside CanActivate - true ")
return true;
}
else {
this.router.navigate(['/pages/login'], { queryParams: { returnUrl:
state.url } });
console.log("inside CanActivate - false -
this.router.navigate(['/pages/login'] ")
return false;
}
}
canActivateChild(route: ActivatedRouteSnapshot, state:
RouterStateSnapshot) {
this.expectedRole = route.data;
console.log("AAAAAA ", this.expectedRole);
if ( localStorage.getItem('Access') == "18") {
this.accessId = true;
localStorage.setItem('accessId',
JSON.stringify(this.accessId));
var output = localStorage.getItem('accessId');
console.log("localStorage.getItem('accessId') = ",output)
return true;
}
else
{
this.router.navigate(['/pages/login'], { queryParams: {
returnUrl: state.url } });
return false;
}
}
}
答案 0 :(得分:0)
路径:' 主系统',!=' 主系统 / update-system'
也许它只是一个错字?