1

I'm trying to use the code in this SO answer, which uses 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?

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.

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

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:

 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
Community
  • 1
  • 1
jmilloy
  • 7,875
  • 11
  • 53
  • 86

1 Answers1

5

Since ComponentMetadata is deprecated, We need to use

var  metadata = new Component(annotation);
Ramkumar S
  • 51
  • 1