Lemmy4555 Lemmy4555 - 2 months ago 30
TypeScript Question

Angular 2 getting error while adding custom decorator with the line "import 'reflect-metadata'"

On my project I was trying to implement a custom decorator for data validation but at a point I got stuck because of this error:

Error: Can't resolve all parameters for LoginService: (?, ?, ?).(…)


Or an error like this in other cases:

Unexpected value 'AppComponent' declared by the module 'AppModule'


Then I started to do some tests and I found out that line causing this error is:

import "reflect-metadata";


Here are two versions of the tour-of-heroes tutorial:

With the error caused by importing reflect-metadata.

And with the solution.

Answer

After a day debugging angular and editing my code I found the solution to this issue only by adding another import:

import "reflect-metadata";

to the class that is mentioned in the error even if the class isn't using any object of reflect-metadata:

e.g. If you are getting an error like these:

Error: Can't resolve all parameters for LoginService: (?, ?, ?).(…)

Or

Unexpected value 'AppComponent' declared by the module 'AppModule'

Add the import line to the classes LoginService and AppComponent to solve the issue.

Here is a working version on Plunker.