【发布时间】:2017-05-08 09:36:26
【问题描述】:
我正在尝试使用this SO answer 中的代码,它使用ComponentMetadata。貌似以前是 angular/core 的,但我猜它不再可用了?
这里是代码。我该怎么做才能获得最后一行中使用的metadata?
function ExtendComponent(annotation: any) {
return function (target: Function) {
var parentTarget = Object.getPrototypeOf(target.prototype).constructor;
var parentAnnotations = Reflect.getMetadata('annotations', parentTarget);
var parentAnnotation = parentAnnotations[0];
Object.keys(parentAnnotation).forEach(key => {
if (isPresent(parentAnnotation[key])) {
// verify is annotation typeof function
if(typeof annotation[key] === 'function'){
annotation[key] = annotation[key].call(this, parentAnnotation[key]);
}else if(
// force override in annotation base
!isPresent(annotation[key])
){
annotation[key] = parentAnnotation[key];
}
}
});
var metadata = new ComponentMetadata(annotation);
Reflect.defineMetadata('annotations', [ metadata ], target);
}
}
我这里真的是在黑暗中拍摄,但是我在角度源中发现了这个测试,它使用了MetadataCollector。
import { MetadataCollector } from '@angular/tsc-wrapped';
...
const collector = new MetadataCollector({quotedNames: true});
...
const metadata = collector.getMetadata(source);
const componentMetadata = metadata.metadata['MyComponent'];
这甚至可以替代吗?我试图检查一下,但在new MetadataCollector({quotedNames: true}) 我得到了
Supplied parameters do not match any signature of call target.
即使我尝试new MetadataCollector(),我也会收到此汇总警告和包更新失败错误:
rollup: Treating 'fs' as external dependency
bundle update failed: Error transforming .../node_modules/typescript/lib/typescript.js
with 'commonjs' plugin: The keyword 'package' is reserved (57066:28) in .../node_modules/typescript/lib/typescript.js
【问题讨论】:
标签: angular