【发布时间】:2019-12-16 14:17:36
【问题描述】:
我正在编写一个 Angular 库。一个对象包装了一个 Angular 组件实例,并且该对象必须订阅组件实例中所有标有 Output 装饰器的主题。
这是我迄今为止在构造函数中编写的代码:
const componentProperties = Object.keys(this.component.instance);
componentProperties.forEach(property => {
if (!!Reflect.getMetadata('Output', this.component.instance, property)) {
//do stuff to subscribe
}
});
Reflect.getMetadata 正在返回 false 还分析角度组件内的属性“testEvent”:
export class Test2Component implements OnInit {
public text: string;
@Output() testEvent = new EventEmitter();
//...
}
我做错了什么?
更新
目前,我使用了这个技巧:
componentProperties.forEach(property => {
if (
this.component.instance.constructor.__prop__metadata__ &&
this.component.instance.constructor.__prop__metadata__[property]
) {
this.component.instance.constructor.__prop__metadata__[property].forEach(decorator => {
if (Reflect.getPrototypeOf(decorator)['ngMetadataName'] === 'Output') {
let eventName = decorator.bindingPropertyName ? decorator.bindingPropertyName : property;
this.registerEvent(property);
this.emitters.push(eventName);
}
});
}
});
很丑,但是可以用
【问题讨论】: