从引用中获取Angular组件的名称

时间:2017-05-17 14:15:02

标签: angular

有没有办法从参考中获取Angular中的组件名称?

所以,有这样的事情:

@Input comp: any;

ngOnInit {
  console.log(this.comp);
}

将记录整个组件对象,但我只想要一个带有组件名称的字符串。

这可能吗?感谢

2 个答案:

答案 0 :(得分:9)

是的,使用this.constructor.name肯定是可能的。这将为您提供当前组件的名称。

this.constructor.name

在您的情况下,您可以尝试

this.comp.constructor.name

答案 1 :(得分:0)

如果要将“ comp”组件作为类传递,并且想要获取该组件的类名。

let compName = this.comp.prototype.constructor.name;