ng2-permission
ng2-permission copied to clipboard
Multiple Modules
I have a problem when I use [hasPermission]="role"
in sub-modules, I imported Ng2Permission
in my app.module
and if I import into another.module
for use that, is created another instance for PermissionService
and all business rules about permissions will been lost.
@JavadRasouli do you have any suggestion?
@AlisonVilela take a look about CoreModules here: ngModule. I think it could solve your problem
Yeah @AlexandreKavalerski , I have a core.module
but when I import Ng2Permission
and PermissionService
don't work in SubModules Routes
For example.:
- core.module
Import Ng2Permission and PermissionService
- app.module
Import core.module
- app.routing.module
Use PermissionGuard
- admin
Directory
-
- admin.module
Import core.module
- admin.module
-
- admin.routing.module
Use PermissionGuard
- admin.routing.module
In start the app, I define the roles this.permissionService.define(this.roles)
PermissionService.store
in add.routing.module
OK
PermissionService.store
in admin.routing.module
always empty
When I import Ng2Permission
in app.module
always routes works fine, including admin.routing.module
but the directive[hasPermission]
don't work in all pages of admin.module
So, when I import Ng2Permission
in core.module
or in admin.module
, routing roles with PermissionGuard
stops working because PermissionService.store
is empty in this SubModule
I thing PermissionService
has been created another instance when import Ng2Permission
in another module
PermissionService
is singletone and there is just one instance available in all modules. i must test your scenario at another time.
Only the root AppModule
should import the CoreModule
or Ng2Permission
. In your code core.module
multiple import. Once in app.module
and again in admin.module
. This will make several instances of the PermissionService
.
@JavadRasouli Ok, but I need to import Ng2Permission
into CoreModule
and I need to import CoreModule
into multiple modules
@AlisonVilela We have the same problem with lazy-loading module. I fix it using this fork https://github.com/mastropinguino/ng2-permission with this instruction: https://stackoverflow.com/questions/39672816/can-lazy-loaded-modules-share-the-same-instance-of-a-service-provided-by-their-p
@jmparra very interesting, @JavadRasouli if we configure forRoot
in this way? we could then import into other modules without duplicating the service
Hi all, I have same issue with lazy load modules also. I see that @JavadRasouli already update ng2-permission.module.ts to add forRoot method in git repo, but yarn or npm still fetchs the old angular2-permission-0.1.3.tgz package which doesn't contain implementation forRoot method (it's same even I get the link and paste to browser). The link I tried: https://registry.yarnpkg.com/angular2-permission/-/angular2-permission-0.1.3.tgz
I already try to clear yarn cache by yarn cache clean angular2-permission but without good result Could you please tell me how can I use the latest implementation for angular2-permission? Thanks in advanced,