Is there a clean way to get a reference to the component of current route?
This seems to work, but seems very hacky:
this.router.currentInstruction.
component.componentType.prototype.somePropertyOrFunction();
Is there a clean way to get a reference to the component of current route?
This seems to work, but seems very hacky:
this.router.currentInstruction.
component.componentType.prototype.somePropertyOrFunction();
Only short (but hacky) way I found is next (Angular 4, 6, 7, 8):
const currentComponent = (router as any).rootContexts.contexts
&& (router as any).rootContexts.contexts.get('primary')
&& (router as any).rootContexts.contexts.get('primary').outlet.component;
BEWARE: that's a HACK, may not work if you have more than one router outlet etc.
You can get the type of the component from a CanActivate guard. For example:
@Injectable()
export class MyGuard: CanActivate
{
canActivate(route: ActivatedRouteSnapshot) : boolean{
let componentType = route.routeConfig.component;
}
}