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

Keith Otto

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.

Günter Zöchbauer

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

No provider for service error in angular2, why do I need to inject it in it's parent component?

From Dev

Inject service in templateUrl in angular component

From Dev

Inject service into angular 1.5 component

From Dev

How To Inject Service Into Angular 1.5 Component (No Typescript)

From Dev

How To Inject Service Into Angular 1.5 Component (No Typescript)

From Dev

error when trying to inject poller advice

From Dev

Inject a service into a component

From Dev

"Unsatisfied dependencies" with UnsatisfiedResolutionException when trying to inject EJB into another component

From Dev

Is it possible to inject a service into Angular 1.5 component's templateUrl property?

From Dev

Angular: How do I inject a service into a component using appInject?

From Dev

Angular: How do I inject a service into a component using appInject?

From Dev

Angular 2: Inject Service to another service. (No provider error)

From Dev

getting karma error when trying to inject firebase in angularjs module

From Dev

getting karma error when trying to inject firebase in angularjs module

From Dev

Why can't Angular2 inject my service?

From Dev

Angular issue when trying to link service to controller

From Java

How to inject service into class (not component)

From Dev

Why do I get an OpenSSL error when trying to connect to Apple Push Notification Service?

From Dev

Why do I get "Could not push back" error when trying to use the IBM Bluemix Document Conversion service?

From Dev

Error when trying to access to a function in my service

From Dev

Angular Unit test fails when i try to inject a service

From Dev

Angular Unit test fails when i try to inject a service

From Dev

Angular Can't Inject Custom Service when Unit Testing Directive

From Dev

Jasmine spyOn service when testing Angular component

From Dev

Spring MVC + Spring Data, error when inject a Repository on Service

From Dev

I receive an error when trying to pass a component/prop into a parent component

From Dev

How to inject upgraded Angular 1 service/factory to Angular 2 component in ES5?

From Dev

Is an angular service provided to a component destroyed when the component is destroyed?

From Dev

How to inject custom service to angular component in plain ES5 (Javascript)?

Related Related

  1. 1

    No provider for service error in angular2, why do I need to inject it in it's parent component?

  2. 2

    Inject service in templateUrl in angular component

  3. 3

    Inject service into angular 1.5 component

  4. 4

    How To Inject Service Into Angular 1.5 Component (No Typescript)

  5. 5

    How To Inject Service Into Angular 1.5 Component (No Typescript)

  6. 6

    error when trying to inject poller advice

  7. 7

    Inject a service into a component

  8. 8

    "Unsatisfied dependencies" with UnsatisfiedResolutionException when trying to inject EJB into another component

  9. 9

    Is it possible to inject a service into Angular 1.5 component's templateUrl property?

  10. 10

    Angular: How do I inject a service into a component using appInject?

  11. 11

    Angular: How do I inject a service into a component using appInject?

  12. 12

    Angular 2: Inject Service to another service. (No provider error)

  13. 13

    getting karma error when trying to inject firebase in angularjs module

  14. 14

    getting karma error when trying to inject firebase in angularjs module

  15. 15

    Why can't Angular2 inject my service?

  16. 16

    Angular issue when trying to link service to controller

  17. 17

    How to inject service into class (not component)

  18. 18

    Why do I get an OpenSSL error when trying to connect to Apple Push Notification Service?

  19. 19

    Why do I get "Could not push back" error when trying to use the IBM Bluemix Document Conversion service?

  20. 20

    Error when trying to access to a function in my service

  21. 21

    Angular Unit test fails when i try to inject a service

  22. 22

    Angular Unit test fails when i try to inject a service

  23. 23

    Angular Can't Inject Custom Service when Unit Testing Directive

  24. 24

    Jasmine spyOn service when testing Angular component

  25. 25

    Spring MVC + Spring Data, error when inject a Repository on Service

  26. 26

    I receive an error when trying to pass a component/prop into a parent component

  27. 27

    How to inject upgraded Angular 1 service/factory to Angular 2 component in ES5?

  28. 28

    Is an angular service provided to a component destroyed when the component is destroyed?

  29. 29

    How to inject custom service to angular component in plain ES5 (Javascript)?

HotTag

Archive