Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current component name

Tags:

angular

Is that possible to get current component name?

for example if I have localhost:3000/en/mycomponent and I'll to this.router.url it returns /en/mycomponent which is /language/component

I want to make redirect on language change.

I can make

this.router.navigate([this.lang, my component here]);

But how can I get current component from routing?

like image 930
gsiradze Avatar asked Oct 12 '16 13:10

gsiradze


3 Answers

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.

like image 182
Ryan_D Avatar answered Oct 31 '22 02:10

Ryan_D


Angular CLI:

You can access the component name from the constructor:

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

 this.constructor.name; // Component name
like image 9
Costly Developer Avatar answered Oct 31 '22 03:10

Costly Developer


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!!

like image 5
Madhu Ranjan Avatar answered Oct 31 '22 03:10

Madhu Ranjan