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?
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
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!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With