简体   繁体   English

替换Angular中的ComponentMetadata

[英]Replacement for ComponentMetadata in angular

I'm trying to use the code in this SO answer , which uses ComponentMetadata . 我正在尝试在此SO答案中使用代码, 该答案使用ComponentMetadata It looks like it used to be in angular/core, but I guess it is no longer available? 看起来它曾经是角形/芯形,但是我想它不再可用了?

Here is the code. 这是代码。 What can I do to get the metadata used in the last line? 如何获得最后一行中使用的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);
  }
}

I'm really shooting in the dark here, but I found this test in the angular source, which uses MetadataCollector . 我在这里确实是在黑暗中拍摄,但是我在使用MetadataCollector的角度源中找到了该测试。

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

Would this even be a replacement? 甚至可以替代吗? I tried to check it out, but at new MetadataCollector({quotedNames: true}) I get 我试图检查一下,但是在new MetadataCollector({quotedNames: true})

Supplied parameters do not match any signature of call target.

And even if I try new MetadataCollector() , I get this rollup warning and bundle update failed error: 即使我尝试使用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

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

var  metadata = new Component(annotation);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM