ChatGPT解决这个技术问题 Extra ChatGPT

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.

Maybe the import? Is '../' an index.ts (Barrel)? Can you try to import it from the file where it is declared directly instead?
Miraculously that seems to have fixed it! Odd that it wouldn't work using the barrel when the other components I tested the service with did. If you want to post that as an answer instead of comment I'll accept it.
Generally a circular dependency.
I've had this issue with circular dependency as well. Its worth noting that newer versions of web pack are much better at telling you this
Looks like circular dependency, If you use angular >=4 so you can get rid of intex.ts (barrel) and import all you need directly.

G
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)


this is correct if you have for example a service injected into another service that first one needs to come first in the barrel.
The Angular2 team doesn't recommend barrels anymore because of a lot of such issues. Glad to hear I could help :)
Didn't know that Angular 2 team doesn't recommend barrels. If so, they should note that in the glossary that discusses their benefit. And projects like angular2-webpack-starter should not use them.
It seams this was fixed (not an issue in 2.0.2). I find barrels still useful, specially when I need to import several models and services between different modules. This import { cleanValues, getState, FirebaseService, NotificationService, ... } from '../../shared/services'; was a pain when it didn't work (; NgModule is no help with singleton services...
Argghh I solved it by changing the order of the exports in the barrel (index.ts) file.. thank you so much
J
J.P.

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.


Yeah this error is typically because you have forgot to add @Injectable and for instance you may just be importing the Router from '@angular/router' and without that injectable, this error will always happen (as soon as you decide to make one line of code use that injected router.
Awesome answer, my issue was that I added a method in my service and didn't add a semi colon after the final brace. I have no idea why it has to be this way yet it's not the case in component classes... for now am only glad to move on!
I had a simple model that gave me this error. I used the shortcut where you create your properties in the constructor of the model. The types of the properties was only string and int. Then suddenly this issue started occurring. Adding @Injectable() fixed the issue. Weird because none of my other models has Injectable added. I have to add that I upgraded quite a few libraries before adding this new model. Possibly had something to do with it, but working now. Thanks.
@Moutono Correct. With an empty constructor in your service, the dependency injection doesn't seem to get triggered and @injectable() isn't required. Which is another oddity on itself. For good measure I'd still add it though, simply for when you should ever decide to inject something.
Bear in mind that if your service has a base class that too needs to be decorated with @Injectable()
Z
Zabavsky

As of Angular 2.2.3 there is now a forwardRef() utility function that allows you to inject providers that have not yet been defined.

By not defined, I mean that the dependency injection map doesn't know the identifier. This is what happens during circular dependencies. You can have circular dependencies in Angular that are very difficult to untangle and see.

export class HeaderComponent {
  mobileNav: boolean = false;

  constructor(@Inject(forwardRef(() => MobileService)) public ms: MobileService) {
    console.log(ms);
  }

}

Adding @Inject(forwardRef(() => MobileService)) to the parameter of the constructor in the original question's source code will fix the problem.

References

Angular 2 Manual: ForwardRef

Forward references in Angular 2


forwardRef() was there already in alpha ;-) It's only needed if Mobile service is declared further down in the same file. If the classes are in different files, there is no need for forwardRef()
Günter Zöchbauer, I am still trying to figure out the real problem with my code, but in the meantime forwardRef() did help to get rid of the Can't resolve all parameters for ... message. At least the component is working while I'm looking for a nicer solution to the problem. (And yes, the failing dependency is imported directly from its file)
@GünterZöchbauer I've updated my answer with references. I'm not trying to take away from your answer, but this actually solves the problem and people need to know about it. If you google the question there are no results that say to use forwardRef. It's very hard to find. Took me all day yesterday to resolve this problem.
Weird. I posted at least a dozen answers that suggest using foreardRef myself but not anymore after NgModule was introduced. I'm not worried about loosing reps. I'm just curious why you run into this after this didn't pop up anymore since a few months. I'll have a closer look when I'm back home in a few days. Thanks a lot for the feedback.
If someone also got lost about the imports: import { Component, Inject, ForwardRefFn, forwardRef } from '@angular/core';
K
KANJICODER

WRONG #1: Forgetting Decorator:

//Uncaught Error: Can't resolve all parameters for MyFooService: (?).
export class MyFooService { ... }

WRONG #2: Omitting "@" Symbol:

//Uncaught Error: Can't resolve all parameters for MyFooService: (?).
Injectable()
export class MyFooService { ... }

WRONG #3: Omitting "()" Symbols:

//Uncaught Error: Can't resolve all parameters for TypeDecorator: (?).
@Injectable
export class MyFooService { ... }

WRONG #4: Lowercase "i":

//Uncaught ReferenceError: injectable is not defined
@injectable
export class MyFooService { ... }

WRONG #5: You forgot: import { Injectable } from '@angular/core';

//Uncaught ReferenceError: Injectable is not defined
@Injectable
export class MyFooService { ... }

CORRECT:

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

but won't this become a singleton? what if I want new instances on each dependency?
C
Community

As already stated, the issue is caused by the export ordering within the barrel which is caused by circular dependencies.

A more detailed explanation is here: https://stackoverflow.com/a/37907696/893630


Ordering didn't solve my problem. Not using the barrel did.
S
Stephen Paul

I also encountered this by injecting service A into service B and vice versa.

I think it's a good thing that this fails fast as it should probably be avoided anyway. If you want your services to be more modular and re-usable, it's best to avoid circular references as much as possible. This post highlights the pitfalls surrounding that.

Therefore, I have the following recommendations:

If you feel the classes are interacting too often (I'm talking about feature envy), you might want to consider merging the 2 services into 1 class.

If the above doesn't work for you, consider using a 3rd service, (an EventService) which both services can inject in order to exchange messages.


This is definitely what happened to me, and this is the way to go. If you know you have more than one service requiring updates, use an EventService. It's more extensible as you'll no doubt have to tap into those events as you extend the application based on these events.
J
JsAndDotNet

For the benefit of searchers; I got this error. It was simply a missing @ symbol.

I.e. This produces the Can't resolve all parameters for MyHttpService error.

Injectable()
export class MyHttpService{
}

Adding the missing @ symbol fixes it.

@Injectable()
export class MyHttpService{
}

In my case, I'd added extra classes and interfaces between @Injectable and the service class definition, so the service class was no longer marked as injectable.
If you completely forget the @Injectable() decorator on a service class that consumes other injectable services, then your improperly decorated service will also throw this error.
S
Stewart_R

Another possibility is not having emitDecoratorMetadata set to true in tsconfig.json

{
  "compilerOptions": {

     ...

    "emitDecoratorMetadata": true,

     ...

    }

}

Can you elaborate on this?
T
Tomasz

In addition to the missing @Injectable() decorator

Missing @Injectable() decorator in abstract class produced the Can't resolve all parameters for service: (?) The decorator needs be present in MyService as well as in the derived class BaseService

//abstract class
@Injectable()
abstract class BaseService { ... }

//MyService    
@Injectable()
export class MyService extends BaseService {
.....
}

This was the case for me.
A
AJ Richardson

In my case, I needed to add import "core-js/es7/reflect"; to my application to make @Injectable work.


This is now 'core-js/features/reflect'. Also, the polyfill is only needed in JIT (dev) mode, so it can be conditionally loaded using dynamic imports and 'magic comments' in webpack. For example: if (process.env.NODE_ENV !== 'production') { import(/* webpackMode: "eager" */ 'core-js/features/reflect').then(() => {});}. See webpack.js.org/api/module-methods/#magic-comments.
M
M K

You get this error if you have service A that depends on a static property / method of service B and the service B itself depends on service A trough dependency injection. So it's a kind of a circular dependency, although it isn't since the property / method is static. Probably a bug that occurs in combination with AOT.


I've also had it when there's a dependency on a function simply defined in the same file. Splitting it into a separate file fixed it.
Thank you for mentioning it. I've found myself in the exact situation. I didn't realized that accessing static classes directly can have something to do with the DI. I had this scheme: A -> B and both of them were using the same static class. The solution with forwardRef helps, but I'm going to look how this could be untangled. I will probably try to make a real service out of this static class (this will lead to a better design as well).
A
Alf Moh

In my case, it happened because I didn't declare the type for a constructor parameter.

I had something like this:

constructor(private URL, private http: Http) { }

and then changing it to the code below solved my problem.

constructor(private URL : string, private http: Http) {}

o
otiai10

This answer might be very helpful for this problem. In addition, for my case, exporting the service as default was the cause.

WRONG:

@Inject()
export default class MobileService { ... }

CORRECT:

@Inject()
export class MobileService { ... }

r
repo

for me it was just lack of () in @Injectable. Proper is @Injectable()


Or in my case accidentally deleting the @
M
Matjaz Hirsman

Removing parameters from injectable constructor() method solved it for my case.


This was exactly my problem too. I came to post it but found you did first! +1
then how to send the parameters to reusable service?
T
Tinh Dang

In my case it was because of the plugin Augury, disable it will work fine. Alternative option is aot, also works.

all credits to @Boboss74 , he posted the answer here: https://github.com/angular/angular/issues/23958


O
Ophir Stern

Well for me the issue was even more annoying, I was using a service within a service and forgot to add it as dependency in the appModule! Hope this helps someone save several hours breaking the app down only to build it back up again


I was just missing an @Inject annotation. I was overlooking exactly what the error message says a bit. If you don't suspect circular dependencies, just go to the class mentioned in the error and look at all the constructor parameters and any class members annotated with @Inject and make sure you're doing DI right on all of them. More on DI here: angular.io/docs/ts/latest/guide/dependency-injection.html
s
sarora

This can be a really difficult issue to debug due to the lack of feedback in the error. If you are concerned about an actual cyclic dependency, here's the most important thing to look at in the stack trace a) the name of the service b) the constructor parameter in that service that has a question mark e.g. if it looks like this:

can't resolve all parameters for AuthService: ([object Object], [object Object], [object Object], [object Object], ?)

then it means the 5th parameter is a service that also depend on AuthService. i.e. question mark, means it wasn't resolved by DI.

From there, you just need to decouple the 2 services by restructuring the code.


L
Leepians

Adding Injectable solved my problem:

Error code

import { Observable } from 'rxjs';
import { io } from 'socket.io-client';
import { HttpClient } from '@angular/common/http';

export class Chat {

After fix

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { io } from 'socket.io-client';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class Chat {

m
maleman

I have encountered this error by mistyping the service's name, i.e. constructor (private myService: MyService).

For misspelled services, I was able to determine which service was the problem (I had several listed in the constructor) by inspecting the page in Chrome->Console. You will see as part of the message a "parameter" array list by displaying object Object, object Object, ? (or something like that). Notice where the "?" is and that is the position of the service that is causing the problem.


S
Shivang Gupta

You have to add providers array in @Component decorator or in the module where your component is declared. Inside component you can do as below:

@Component({
  moduleId: module.id,
  selector: 'pm-header',
  templateUrl: 'header.component.html',
  styleUrls: ['header.component.css'],
  directives: [ROUTER_DIRECTIVES, NgClass],
  providers: [MobileService]
})

S
Satendra

In my case passing wrong parameters to constructor generates this error, basic idea about this error is that you unknowingly passed some wrong args to any function.

export class ProductComponent {
    productList: Array<Product>;

    constructor(productList:Product) { 
         // productList:Product this arg was causing error of unresolved parameters.
         this.productList = [];
    }
}

I solved this by just removing that argument.


A
Ahmed Elkoussy

For me, I got this error when I mistakenly disabled this import in the polyfills.ts file , you need to ensure it is imported to avoid that error.

/** Evergreen browsers require these. **/
// Used for reflect-metadata in JIT. If you use AOT (and only Angular decorators), you can remove.
import 'core-js/es7/reflect';

M
Muhammed Ozdogan

In my case I was trying to extend "NativeDateAdapter" in order to override "format(date: Date, displayFormat: Object)" method.

In AngularMaterial-2 DatePicker .

So basically I forgot to add @Injectable anotation.

After I add this to my "CustomDateAdapter" class:

@Injectable({
  providedIn: 'root'
})

Error has gone.


This worked for me, but I have no idea why. Do both the service and the component which receives it through DI need to be providedIn: root in order for DI to work?
C
Cristian Traìna

Gotcha!

If none of the above answers helped you, maybe you are importing some element from the same file where a component is injecting the service.

I explain better:

This is the service file:

// your-service-file.ts
import { helloWorld } from 'your-component-file.ts'

@Injectable()
export class CustomService() {
  helloWorld()
}

This is the component file:

@Component({..})
export class CustomComponent {
  constructor(service: CustomService) { }
}

export function helloWorld() {
  console.log('hello world');
}

So it causes problems even if the symbol isn't inside the same component, but just inside the same file. Move the symbol (it can be a function, a constant, a class and so on...) elsewhere and the error will fade away


l
lolcatzftw

for angular 6 and newer versions, try

@Injectable({
  providedIn: 'root'
})

..right above your service class with no other lines in between

advantages

no need to add the service to any module (will be "auto-discovered")

service will be a singleton (since it will be injected into root)

[angular docs]


C
Community

Although the ordering of exported classes from within barrels may have been mentioned, the following scenario may also produce the same effect.

Suppose you have classes A, B, and C exported from within the same file where A depends on B and C:

@Injectable()
export class A {
    /** dependencies injected */
    constructor(private b: B, private c: C) {}
}

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

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

Since the dependent classes (i.e. in this case classes B and C) are not yet known to Angular, (probably at run-time during Angular's dependency injection process on class A) the error is raised.

Solution

The solution is to declare and export the dependent classes before the class where the DI is done.

i.e. in the above case the class A is declared right after its dependencies are defined:

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

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

@Injectable()
export class A {
    /** dependencies injected */
    constructor(private b: B, private c: C) {}
}

M
Menelaos Kotsollaris

In my case, I was exporting a Class and an Enum from the same component file:

mComponent.component.ts:

export class MyComponentClass{...}
export enum MyEnum{...}

Then, I was trying to use MyEnum from a child of MyComponentClass. That was causing the Can't resolve all parameters error.

By moving MyEnum in a separate folder from MyComponentClass, that solved my issue!

As Günter Zöchbauer mentioned, this is happening because of a service or component is circularly dependent.


S
Simon_Weaver

If your service is defined in the same file as a component (that consumes it) and the service is defined after the component in the file you may get this error. This is due to the same 'forwardRef' issue others have mentioned. At this time VSCode isn't great about showing you this error and the build compiles successfully.

Running the build with --aot can mask this problem due to the way the compiler works (probably related to tree shaking).

Solution: Make sure the service is defined in another file or before the component definition. (I'm not sure if forwardRef can be used in this case, but it seems clumsy to do so).

If I have a very simple service that is very strongly tied to a component (sort of like a view model) - eg. ImageCarouselComponent, I may name it ImageCarouselComponent.service.ts so it doesn't get all mixed up with my other services.


l
lucbonnin

In my case it was a circular reference. I had MyService calling Myservice2 And MyService2 calling MyService.

Not good :(


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now