In my app I have a parent route with a series of child routes:
{
path: 'parent',
component: ParentComponent,
children: [
{ path: 'child1', component: Child1 },
{ path: 'child2', component: Child2 }
]
}
I have a non-global service (MyService) setup as a provider in the ParentComponent, which is injected into both of the child components.
Now I want to add a route guard to the second child component, making the route definition like this:
{
path: 'parent',
component: ParentComponent,
children: [
{ path: 'child1', component: Child1 },
{ path: 'child2', component: Child2, canActivate: [MyGuard] }
]
}
How can I access MyService from within MyGuard
? If I try to inject it in normally, I get the no provider for MyService
error.
I see the canActivate
route guard can accept an route: ActivatedRouteSnapshot
parameter, and this parameter has a component
property. However, this property isn't an instance of the component, it just seems to be the class definition for the component. Am I supposed to create an instance of the component myself?
How can I get access to the service I require?