[router] CanActivate and DI
See original GitHub issueThe CanActivate
hook is available as a decorator.
That’s a bit surprising compared to all the other lifecycle hooks which are simple functions of the component. I understand the reason behind that (the hook is called before the component is instanciated), even if I think the API would be simpler if everything was a function…
If things stay like this, I have a question: will it be possible to use DI in the CanActivate
hook?
Do you plan to support something like :
@CanActivate((userService: UserService) => userService.isAdmin())
Issue Analytics
- State:
- Created 8 years ago
- Reactions:31
- Comments:91 (41 by maintainers)
Top Results From Across the Web
CanActivate - Angular
Interface that a class can implement to be a guard deciding if a route can be activated. If all guards return true ,...
Read more >Introducing the CanMatch Router Guard In Angular
Angular provides the canLoad and canActivate router guards. CanLoad and canActivate guards are used to ... Unleash the Power of DI Functions in...
Read more >angular dependency injection of one service into another for ...
In my angular app I have two services, one deals with the http layer, and the other is simply a route guard to...
Read more >Angular Basics: CanActivate—Introduction to Routing Guards
This post has introduced you to guards in Angular routing and the various types that exist. We have also seen the way to...
Read more >Angular CanActivate vs. Interceptors— A Comprehensive Guide
Synchronous: Verifies that the current user has privileges to access the route, using an RBAC (Role-Based Access Control) system or by simply ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top 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
@btford I think there should be a way to provide a class to the CanActivate instead of a pure function and pass that class through the injector. This could be used for resolving providers also.
Proposal
@frederikschubert I have a simple workaround here. I store the application injector and use it to inject dependencies inside the CanActivate function.
http://plnkr.co/edit/SF8gsYN1SvmUbkosHjqQ?p=preview