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);
        });
}
Max Solid
  • 1,213
  • 3
  • 21
  • 32
  • I'd expect that `data` only becomes available after `canActivate` has returned `true` – Günter Zöchbauer Mar 07 '17 at 15:56
  • 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 working – Max Solid Mar 07 '17 at 16:06
  • 1
    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 fail – Aluan Haddad Mar 07 '17 at 16:14
  • 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! – Luke Baughan Apr 21 '17 at 15:40

1 Answers1

12

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

G.Vitelli
  • 1,229
  • 9
  • 18