Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when trying to inject a service into an angular component "EXCEPTION: Can't resolve all parameters for component", why?

I've built a basic app in Angular, but I have encountered a strange issue where I cannot inject a service into one of my components. It injects fine into any of the three other components I have created, however.

For starters, this is the service:

import { Injectable } from '@angular/core';  @Injectable() export class MobileService {   screenWidth: number;   screenHeight: number;    constructor() {     this.screenWidth = window.outerWidth;     this.screenHeight = window.outerHeight;      window.addEventListener("resize", this.onWindowResize.bind(this) )   }      onWindowResize(ev: Event) {     var win = (ev.currentTarget as Window);     this.screenWidth = win.outerWidth;     this.screenHeight = win.outerHeight;   }    } 

And the component that it refuses to work with:

import { Component, } from '@angular/core'; import { NgClass } from '@angular/common'; import { ROUTER_DIRECTIVES } from '@angular/router';  import {MobileService} from '../';  @Component({   moduleId: module.id,   selector: 'pm-header',   templateUrl: 'header.component.html',   styleUrls: ['header.component.css'],   directives: [ROUTER_DIRECTIVES, NgClass], }) export class HeaderComponent {   mobileNav: boolean = false;    constructor(public ms: MobileService) {     console.log(ms);   }  } 

The error I get in the browser console is this:

EXCEPTION: Can't resolve all parameters for HeaderComponent: (?).

I have the service in the bootstrap function so it has a provider. And I seem to be able to inject it into the constructor of any of my other components without issue.

like image 603
Keith Otto Avatar asked Jun 23 '16 17:06

Keith Otto


People also ask

Which decorator can be used to prevent angular application from breaking if dependency is not resolved?

The Inject decorator is a constructor parameter used to specify a custom provider of a dependency.

What is inject in angular?

Dependency injection, or DI, is one of the fundamental concepts in Angular. DI is wired into the Angular framework and allows classes with Angular decorators, such as Components, Directives, Pipes, and Injectables, to configure dependencies that they need.


2 Answers

Import it from the file where it is declared directly instead of the barrel.

I don't know what exactly causes the issue but I saw it mentioned several times (probably some kind of circular dependency).

It should also be fixable by changing the order of the exports in the barrel (don't know details, but was mentioned as well)

like image 97
Günter Zöchbauer Avatar answered Sep 28 '22 20:09

Günter Zöchbauer


In addition to the previous answers given, it seems this error is thrown as well when your injectable service is missing the actual @Injectable() decorator. So before you debug the cyclic dependency thing and the order of your imports/exports, do a simple check whether your service actually has @Injectable() defined.

This applies to the current Angular latest, Angular 2.1.0.

I opened an issue on this matter.

like image 45
J.P. ten Berge Avatar answered Sep 28 '22 21:09

J.P. ten Berge