如果我致电Ember.inspect(component)
,我会收到如下回复:
<app@component:my-component::ember1246>
这表明组件知道自己的名字(my-component
)。有没有办法只访问该名称?
答案 0 :(得分:7)
Ember.inspect()
调用对象toString()
,后者又调用一些内部余烬金属函数来派生名称。
然而,人们一直在使用内部属性来获取名称:
this.__proto__._debugContainerKey
这会输出component:my-component
,然后您可以在:
上分开。这个问题将创建一种公开的方式来展示我们将来能够使用的对象元信息:https://github.com/emberjs/ember.js/issues/10742
答案 1 :(得分:0)
这个regex
可以解决问题:
//Returns a string with full name of component, like: <ProjectName@component:path/to/component-name::ember793>
let componentName = this.toString ();
//This regex will match the component and capture the latest part of component path.
let regex = componentName.match (/<[a-zA-Z]+@[a-zA-Z]+:(?:[a-z]+[\/])*([-a-z]+)::[a-zA-Z]+[0-9]+>/);
//The latest element of array is the component name.
console.log (regex[regex.length-1]); //component-name
有关其工作原理的完整说明,请参阅https://regex101.com/r/rX9bQ7/3。