Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5: Can't resolve all parameters for component

I've encountered a problem with injecting services in angular 5 as in tutorial.

I have a simple service

@Injectable()
export class SimpleService {
    ...
}

And a simple component:

@Component({...})
export class SimpleComponent {
    constructor(private simpleService SimpleService) {}
}

A service is set to providers in module:

@NgModule({
    ...
    providers: [SimpleService]
    ...
})
export class SimpleModule {}

I see the following error in console:

Error: Can't resolve all parameters for SimpleComponent

However, if I inject the SimpleService with @Inject like

@Inject(SimpleService) private simpleService: SimpleService

the error disappears.

What am I doing wrong?

UPDATE: I saw several answers where people advise to add emitDecoratorMetadata: true to tsconfig file. But this line is already there

like image 519
Andy J. Avatar asked Jun 15 '18 18:06

Andy J.


People also ask

How to resolve “can't resolve all parameters for appcomponent” error?

Also, you can face this error after updating the Angular version: Can't resolve all parameters for AppComponent: (?) Most сases can be resolved by adding following line in the tsconfig.json: With this parameter, you can forget about adding @Inject () to your function parameters. For some versions of Angular, it should be enabled by default.

What does the (?) error message mean in Angular 2?

The (?) indicates that Angular couldn’t resolve the second parameter of the constructor: As usual, I was using the injection token with the Inject decorator of Angular.

Does Ng serve work well with angular 5?

Bookmark this question. Show activity on this post. Recently I picked up Angular 5 and I was tasked with creating somewhat of a demo-application. Ng serve worked perfectly well and even ng build wasn't giving me any problems.

Does angular no longer need barrel injections?

It’s no wonder that the Angular team is not so fond of barrels anymore (afaik). So in the end, the fixed imports were simply relative ones: From that point on, no more injection issue!


2 Answers

It turned out that the reason for this error was an incorrect import of polyfills.ts. Previously I ejected my cli app and removed polyfills entry point from my webpack config. After that I imported them in my main.ts. But I imported polyfills after I imported AppModule. After I moved import './polyfills.ts' statement to the top of the file the error disappeared.

Another option for fixing this error is moving to AOT compilation:

module: {
    rules: [
        ...
        {
            "test": /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/,
            "use": [
                "@ngtools/webpack"
            ]
        }
    ]
}
plugins: [
    ...
    new AngularCompilerPlugin({
        mainPath: 'main.ts',
        platform: PLATFORM.Browser,
        hostReplacementPaths: {
            'environments/environment.ts': 'environments/environment.ts'
        },
        sourceMap: true,
        tsConfigPath: 'src/app/tsconfig.json',
        skipCodeGeneration: false // this line is responsible for AOT
    })
]

If you use AOT compilation instead JIT you won't need emitDecoratorMetadata in your tsconfig.

like image 129
Andy J. Avatar answered Dec 04 '22 00:12

Andy J.


I had a similar issue and this may help others, as this was the closest match I found while searching.

I had an error in my constructor function. I forgot to declare the type of the Service:

I had written:

constructor(
    private myService
){}

instead of:

constructor(
    private myService: MyService
){}
like image 38
ndvo Avatar answered Dec 04 '22 00:12

ndvo