question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Different instances of Ability in root and lazy loaded module

See original GitHub issue

I’m using with angular and i’ve just upgraded the version of the package and it stopped working. @casl/ability: 2.4.2 -> 2.5.1 @casl/angular: 0.3.1 -> 0.4.1 When i downgraded back to the previous version it worked again. I’m trying to find if there is any breaking change between these versions but I did not find anything related. Below is a part of my code:

// defining the rules
import { AbilityBuilder, Ability } from '@casl/ability';
constructor(private ability: Ability) {}
rules() {
  const { rules, can } = AbilityBuilder.extract();
  can('create', 'customer');
  can('delete', 'customer');
  this.ability.update(rules);
}
// app.module.ts
import { NgModule } from '@angular/core'
import { AbilityModule } from '@casl/angular'
// ...

@NgModule({
  imports: [
    ...,
    AbilityModule
  ],
  declarations: [...],
  bootstrap: [...],
})
export class AppModule {}
<button mat-raised-button color="primary" class="add-button" (click)="addCustomer()" *ngIf="'customer' | can: 'create'">
  <mat-icon>add</mat-icon>
  {{ 'shared.addCustomer' | translate }}
</button>

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
stalniycommented, Dec 2, 2018

I see. The issue with lazy loaded modules fixed in @casl/angular@1.0.0. You need to use AbilityModule.forRoot() in the main module (AppModule) and AbilityModule in lazy loaded or child modules.

I added a note about this in README for @casl/angular

0reactions
stalniycommented, Dec 2, 2018

Thanks for the issue!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Does angular creating a 2 instances of service while Lazy ...
See this answer for a possible solution: how to share same service instance between 2 lazy loaded modules - not at root.
Read more >
Angular Services, providedIn and Lazy Modules - | juri.dev
Let's explore some of the particularities of the Angular dependency injection mechanism.
Read more >
Avoid Multiple Service Instances in Lazy Loaded Modules in ...
As a result, importing shared modules in a lazy loaded module, may result in multiple instances of a service being available in the...
Read more >
Customize Angular lazy loading modules for multiple frontends
In this article, we will explore how to build an Angular app with multiple frontends by utilizing lazy loading feature modules and dynamic ......
Read more >
Angular's providedIn: "root". What if two lazy modules provided ...
It puts that code in its own common module. Then whichever lazy module loads first, downloads the common module along with the lazy...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found