Get component instance on active route in Angular2 router?
I know that ActivatedRouteSnapshot.component
in Angular2 contains the link / class of the activated route if I call Router.routerState.snapshot
, but how do I get for the component class the current (or minimum: one created) instance for it?
Injector.get(..)
doesn't return component instances and creating a new component instance doesn't help (me) either.
source to share
you can use
<router-outlet
(activate)='onActivate($event)'
(deactivate)='onDeactivate($event)'></router-outlet>
where $event
is an instance of a component and for example assigns it to a service to make it globally available.
See also https://angular.io/api/router/RouterOutlet
You can also create your own <router-outlet>
component that does this automatically.
source to share