Different instances of Ability in root and lazy loaded module
See original GitHub issueI’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:
- Created 5 years ago
- Comments:8 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I see. The issue with lazy loaded modules fixed in
@casl/angular@1.0.0
. You need to useAbilityModule.forRoot()
in the main module (AppModule) andAbilityModule
in lazy loaded or child modules.I added a note about this in README for @casl/angular
Thanks for the issue!