Angular2 Exploring Resolved Data in canActivate Guard?
Asked Answered
W

1

15

Is it possible to access resolved data of a route (-Resolver) inside a canActivate guard. Currently I can access the resolved data in the component by

ngOnInit() {
    this.route.data
        .subscribe((data: { example: Array<Object> }) => {
            this.example = data.example;
            console.log('example resolver', this.example);
        });
}

How could I manage that in the canActivate guard? This is not working:

constructor(private route: ActivatedRoute) {}

canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot,
): boolean {

    this.route.data
        .subscribe((data: { example: Array<Object> }) => {
            this.example = data.example;
            console.log('example resolver', this.example);
        });
}
Whig answered 7/3, 2017 at 15:55 Comment(4)
I'd expect that data only becomes available after canActivate has returned trueLoppy
nope ... setting routes data.example manually makes it available in the canActivate by using this.route.data['example'] ... problem seems to be that canActivate runs before data is being resolved by the resolver and the posted subscribe is not workingWhig
You could move the guard logic into the resolve. But of course that's a far less elegant solution. From what I understand all resolves are optional. You can block navigation by having the resolve failInrush
I have the same issue, I have authGuard taking a list of allowed roles from the data: part (works fine) but Im trying to resolve the users currently assigned roles for checking in authGuard against the "allowed" roles - the data part (list of allowed roles) works fine, I cannot access the "resolved" roles however - resolve doesnt even seem to be ing called!Faceharden
D
13

No, you can't because canActivate Method is called before resolve, so you can't get the data

Guard Processing:

  1. canDeactivate

  2. canLoad

  3. canActivateChild

  4. canActivate

  5. resolve

Deglutition answered 9/7, 2018 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.