Get current component name

You may get the component name like below,

export class MyComponent{

  constructor(
    private route: ActivatedRoute,
    private router: Router
  ) {
    // Below will result in MyComponent
    console.log(this.route.component.name);         
  }
}

Having said that Routing is based on path rather the component name.

Hope this helps!!


I was running into the same issue as above and was able to use the following to retrieve the component name: this.route.routeConfig.component.name. This returned a string value that we can compare.


Angular CLI:

You can access the component name from the constructor:

 console.log('this', this.constructor.name);

 this.constructor.name; // Component name

Tags:

Angular