Reputation: 887
I'm having quite a headache because of the following. Hope you can point me in the right direction.
I'm working with Angular 2's ActivatedRouteSnapshot from angular/router in a custom Guard. The route where this guard is called has different data from a resolver. However when I try to access this data, this data is not found, but it is there. Let me clarify with a real example:
app.routing
(...)
path: "businessrules", component: TabsComponent, data: { title: "Business Rules Configuration" }, canActivate: [AuthGuard, RoleGuard], resolve: {
environmentsList: EnvironmentsResolver,
pagesList: PagesResolver
}
(...)
role-guard.service
@Injectable()
export class RoleGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {
}
canActivate(route: ActivatedRouteSnapshot) {
console.log(route); //here, inside route, you can find data, and inside it, environmentList and pagesList
console.log(route.data); //but here, this data can't be found
return true;
}
}
console.log(route)
(...)
Object:
environmentsList:Array[4]
pagesList:Array[6]
title: "Business Rules Configuration"
__proto__:Object
(...)
console.log(route.data)
(...)
Object
title: "Business Rules Configuration"
__proto__: Object
(...)
Both logs are for the same route, so, am I missing something? Sorry if the post is a bit messy, gladly will try to clarify if something is weird.
Thanks.
EDIT A colleague told me I needded to subscribe to route.data, but it seems I can't subscribe nor map an ActivatedRouteSnapshot, only a ActivatedRoute. Also, the method canActivate requires ActivatedRouteSnapshot, so I can't use ActivatedRoute, and I can't use ActivatedRoute in the constructor as it will only be executed once and I need it to be executed per route. Any idea on how to make this work?
Upvotes: 2
Views: 2146
Reputation: 1
Hey I was also stuck with similar problem.
I have a workaround see if that helps your purpose and those who come looking for similar problem.
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
window.location.href = route.root.children[0].routeConfig.resolve.url;
return true;
}
it is available under route.root.children[i].routeConfig.resolve
I couldn't get data but passing the resolve in route served the purpose.
resolve: {
url: "<myurl>"
}
Upvotes: 0