You can import library in any Angular application by running:
$ npm install ngx-role-permissions --save
and then from your Angular AppModule:
use NgxPermissionModule with withElements in any of your modules
use LockTypes.UNLOCKABLE provide roles for which elements will be available
LockTypes.UNLOCKABLE tells if current element will be block with specified roles
As anther aproach provide PERMISSION_CONFIG_TOKEN token with doorlock
use lockWith declaration to declare blocking keys
use unlockWith as an opposite to lockWith
Providing initial roles:
INITIAL_ROLES is multitoken that can provide initial roles
@NgModule({
...
imports: [
// Specify your library as an import
NgxPermissionModule.withElements([
...
]),
],
providers:[
provide: INITIAL_ROLES,
multi: true,
useValue: ['user'],//your roles
]
})
exportclassAppModule{}
SharedModule
If you use a SharedModule that you import in multiple other feature modules, you can export the NgxPermissionModule to make sure you don't have to import it in every module.
NgxPermissionModule with or without withElementsnotations provides canPermit and canNotPermit directive so you should import it in every module you use permission directives.
@NgModule({
exports:[
CommonModule,
NgxPermissionModule,
]
})
exportclassSharedModule{}
Once your library is imported, you can use its components, directives and pipes in your Angular application:
Import service to the main application and load permissions
Usage in routing guards:
permissionConfig placed in data declare route configuration.
permission element - name of element which defined in module.
redirectRoute - router path to redirect page if current route is blocked.
If redirectRoute was not defined no redirect will be done