0

I want to get the name of component which is active by router link in header layout. and then make a check based on the component name active. If there is a way, please help me out sort of this situation. currently I m fetching the router link address in ngOnInit method

this.router.url

I also want the component name that is linked to active router link, because in some cases component names and url segments are not matched in my application

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Usman Hafeez
  • 357
  • 1
  • 7
  • 20

2 Answers2

2
  1. You can use:

    // router-outlet> (activate)='onActivate($event)' (deactivate)='onDeactivate($event)'>

where $event is the component instance. Taken from here https://stackoverflow.com/a/45431729/6528560

  1. You should add data with component name to each route definition, and then just read it in ActivatedRoute

    public constructor(private route:ActivatedRoute) { console.log(route.snapshot.data['name']); }

https://stackoverflow.com/a/40863833/6528560

Oleksandr Poshtaruk
  • 2,062
  • 15
  • 18
2

Hope this will helps you.

this.route.routeConfig.component.name

It will return component name.

  • 3
    This does not return the expected component when using a production build, as the component name has been minified if --optimization = true flag set. – Sean Halls Mar 24 '20 at 11:41