2

In this plunker I'm attempting to decorate a property on MyModel with a custom annotation. Un-commenting the property with custom decorator or un-commenting import 'reflect-metadata'; and the property with inline decorator generates:

Error: Cannot resolve all parameters for 'Parser'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'Parser' is decorated with Injectable. at NoAnnotationError.ZoneAwareError

I can un-comment just the inline decorator but in an editor without the import 'reflect-metadata'; you don't get code autocomplete and tsc error about metadata() not being a property of Reflect.

What is the correct way to use the reflect API in angular?

Edit:
With a new @angular/cli genrated app you have import 'core-js/es7/reflect'; in pollyfills.ts but you still get Property 'metadata' does not exist on type 'typeof Reflect'. if you try any combination of Reflect.metadata()

Edit2: I seemingly have this working in angular cli by replacing import 'core-js/es7/reflect'; with import 'reflect-metadata'; in pollyfills.ts but I don't know if they are one for one replacements or if there is ramifications. The answer here isn't clear to me. Angular2 using either npm reflect-metadata or core-js/es7/reflect

Community
  • 1
  • 1
Wilhelmina Lohan
  • 2,803
  • 2
  • 29
  • 58
  • I just ran into this error and you're the only I've found that has had it so far. I don't need to import anything for it to work, but of course tsc complains that `defineMetadata` doesn't exist on `Reflect`. I'm at a loss – Adam Keenan Feb 21 '17 at 23:08

1 Answers1

1

I just stumbled upon this article which might help. I guess the part where "Second Problem" is addressed might be interesting to you. I faced the same problem trying to define some metadata in an angular application and it seems that angular is providing a different Reflect than window does. So accessing windows' reflect seems to fix the issue for me. I used:

let reflect = window['Reflect'];
reflect.defineMetadata(metadataKey, metadataValue, target);

instead of

Reflect.defineMetadata(metadataKey, metadataValue, target);
Jay
  • 175
  • 1
  • 6