替换Angular中的ComponentMetadata

jmi*_*loy 1 angular

我试图使用的代码在此SO答案,它的用途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);
  }
}
Run Code Online (Sandbox Code Playgroud)

我在这里确实是在黑暗中拍摄,但是我在使用的角度源中找到了该测试MetadataCollector

import { MetadataCollector } from '@angular/tsc-wrapped';
...
const collector = new MetadataCollector({quotedNames: true});
...
const metadata = collector.getMetadata(source);
const componentMetadata = metadata.metadata['MyComponent'];
Run Code Online (Sandbox Code Playgroud)

甚至可以替代吗?我试图检查一下,但是new MetadataCollector({quotedNames: true})我得到了

Supplied parameters do not match any signature of call target.
Run Code Online (Sandbox Code Playgroud)

即使我尝试了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
Run Code Online (Sandbox Code Playgroud)

小智 5

由于不赞成使用ComponentMetadata,因此我们需要使用

var  metadata = new Component(annotation);
Run Code Online (Sandbox Code Playgroud)