可以获取当前组件的名称吗?获取当前组件的名称
例如,如果我有localhost:3000/en/mycomponent
,我会以this.router.url
返回/en/mycomponent
这是/language/component
我想要重定向语言的变化。
我可以让
this.router.navigate([this.lang, my component here]);
但我怎么可以从路由获取电流分量?
可以获取当前组件的名称吗?获取当前组件的名称
例如,如果我有localhost:3000/en/mycomponent
,我会以this.router.url
返回/en/mycomponent
这是/language/component
我想要重定向语言的变化。
我可以让
this.router.navigate([this.lang, my component here]);
但我怎么可以从路由获取电流分量?
我遇到了与上述相同的问题,并能够使用以下命令检索组件名称: this.route.routeConfig.component.name。这返回了一个我们可以比较的字符串值。
你可能会得到该组件名称,如下面,
export class MyComponent{
constructor(
private route: ActivatedRoute,
private router: Router
) {
// Below will result in MyComponent
console.log(this.route.component.name);
}
}
说了这么多路由基于路径,而组件的名称。
希望这有助于!
只有组件名称是不够的。你可能需要的其他选项等..
如果第一段永远是你的language
,像这样做:
let urlSegments = this.route.url.split('/');
urlSegments[1] = this.lang;
this.router.navigate(urlSegments.join('/'));
您确切的要求是什么?它不清楚你的问题。你可以解释吗? – micronyks
可以获取组件名称。但是,当前组件名称是什么意思? – micronyks
@micronyks当前组件名称意味着我想要检测哪个组件是I.我想从服务 中调用它,例如,如果我位于'/ en/homecomponent'中我想要获取名称,它是'homecomponent ' – gsiradze