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.

[router] CanActivate and DI

See original GitHub issue

The 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:closed
  • Created 8 years ago
  • Reactions:31
  • Comments:91 (41 by maintainers)

github_iconTop GitHub Comments

23reactions
brandonrobertscommented, Feb 23, 2016

@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

@Injectable()
class Authenticated implements BeforeActivate {
    constructor(public router: Router) {}

    routerBeforeActivate(next: ComponentInstruction, prev: ComponentInstruction) {
        if (notLoggedIn) {
            this.router.navigate(['/Login']);
            return false;
        }

        return true; // or return promise that resolves to true
    }
}


@CanActivate(Authenticated)
class ProtectedRoute{}
13reactions
brandonrobertscommented, Nov 4, 2015

@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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

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