【问题标题】:Replacement for ComponentMetadata in angular以角度替换 ComponentMetadata
【发布时间】: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


    【解决方案1】:

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

    var  metadata = new Component(annotation);
    

    【讨论】:

      猜你喜欢
      • 2019-04-18
      • 1970-01-01
      • 1970-01-01
      • 2017-10-08
      • 2018-11-28
      • 2019-07-02
      • 2019-11-17
      • 2021-06-14
      • 2017-03-10
      相关资源
      最近更新 更多