NgxPermissionGuard(canActivateChild) throws error on route with no data.permissions
blacksider opened this issue · 5 comments
I'm submitting a...
[ ] Bug report
Current behavior
Route redirect throws error like this:
core.js:5980 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'only' of undefined
TypeError: Cannot read property 'only' of undefined
at NgxPermissionsGuard.transformPermission (ngx-permissions.js:608)
at NgxPermissionsGuard.hasPermissions (ngx-permissions.js:598)
at NgxPermissionsGuard.canActivateChild (ngx-permissions.js:591)
Where it throws this error is a empty route with data set but no permissions, and the line in function hasPermissions
const routeDataPermissions = !!route && route.data ? route.data.permissions : {};
returns undefined
Expected behavior
Do not return undenfined, such as change that line to:
const routeDataPermissions = !!route && route.data ? (route.data.permissions as NgxPermissionsRouterData || {}) : {};
to return empty object.
Minimal reproduction of the problem with instructions
This is my route configurations, I have a parent route lazyloading a subroute
parent.route
:
const routes: Routes = [
{
path: 'parent',
data: {
permissions: {}
},
canActivateChild: [NgxPermissionsGuard],
children: [
{
path: 'configure',
loadChildren: () => import('./child/child.module').then(m => m.ChildModule),
data: {
permissions: {
only: ['ROOT']
}
}
}
]
}
];
child.route
:
const routes: Routes = [
{
path: '',
component: NavComponent,
data: {
},
children: [
{
path: 'info',
data: {
<<<<==== this is where it get permissions as undefined
},
children: [
{
path: 'list',
component: ChildComponent,
data: {
}
}
]
}
]
}
];
Environment
Angular version: 11.1.0
ngx-permissions version: 8.1.1
Browser:
- [ ] Chrome (desktop) version 88.0.4324.96
Hi @blacksider this is angular restriction more in this issue #148 (comment)
Thanks for point out @AlexKhymenko. It used to work perfectly, any idea why this is happenning? I still dont't understand which part angular's been glitchy like this.
@blacksider This is not a glitch this is their design decision.You pass guard to canActivateChild. And then You have to specify data for every child. Its counter intuitive :-(
I would recommend using canLoad here https://angular.io/api/router/CanLoad
@AlexKhymenko ah, ok, got it, thanks for the explanation!