ng2-permission icon indicating copy to clipboard operation
ng2-permission copied to clipboard

Multiple Modules

Open AlisonVilela opened this issue 7 years ago • 8 comments

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 avatar Oct 27 '17 06:10 AlisonVilela

@AlisonVilela take a look about CoreModules here: ngModule. I think it could solve your problem

AlexandreKavalerski avatar Oct 27 '17 13:10 AlexandreKavalerski

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.routing.module Use PermissionGuard

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

AlisonVilela avatar Oct 27 '17 13:10 AlisonVilela

PermissionService is singletone and there is just one instance available in all modules. i must test your scenario at another time.

JavadRasouli avatar Oct 30 '17 10:10 JavadRasouli

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 avatar Nov 12 '17 17:11 JavadRasouli

@JavadRasouli Ok, but I need to import Ng2Permission into CoreModule and I need to import CoreModule into multiple modules

AlisonVilela avatar Nov 13 '17 15:11 AlisonVilela

@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

jmparraguez-haulmer avatar Nov 20 '17 15:11 jmparraguez-haulmer

@jmparra very interesting, @JavadRasouli if we configure forRoot in this way? we could then import into other modules without duplicating the service

AlisonVilela avatar Nov 21 '17 11:11 AlisonVilela

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,

nhthuan avatar May 03 '18 09:05 nhthuan