以角度替换ComponentMetadata

时间:2016-12-22 17:18:16

标签: angular

我正在尝试使用this SO answer中使用ComponentMetadata的代码。看起来它曾经是角度/核心,但我想它已经不再可用了?

这是代码。我该怎么做才能在最后一行中使用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

1 个答案:

答案 0 :(得分:4)

由于不推荐使用ComponentMetadata,我们需要使用

var  metadata = new Component(annotation);