4

I am trying to inject a self created service & the angular2 Http service into my custom HttpRest service.

using

@Inject(Http) public _http: Http

worked fine, but when I try to inject another self made service i get following Error:

EXCEPTION: Cannot resolve all parameters for 'HttpRest'(Http @Inject(Http), undefined @Inject(undefined)). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'HttpRest' is decorated with Injectable.

For some reason UserIds is undefined, even though the import is succesful. My custom service:

@Injectable()
export class UserIds{
   private _signature_id:string;
   private _role_id:number;
   get signature_id():string{
      return this._signature_id;
   }
   set signature_id(id:string){
      this._signature_id = id;
   }
   get role_id():number{
      return this._role_id;
   }
   set role_id(id:number){
      this._role_id = id;
   }
} 

The Custom HttpRest service im injecting both Http & UserIds into:

@Injectable()
export class HttpRest{
    groups;
    constructor(
        @Inject(Http) public _http: Http,
        @Inject(UserIds) public _ids: UserIds
    ){}
...
}

NOTE! when I remove

,
@Inject(UserIds) public _ids: UserIds

I dont get the Error.
What am I missing ?

UPDATE The problem is actually that UserIds is undefined in the constructor params for some unknown reason that im trying to understand, so the title of this question becomes irrelevant. It should be "Imported service is undefined in constructors params".
Please reffer to my answer on this question further down this post.

UPDATE:
Please reffer to a question that discusses this issue. Using index.ts file to export class causes undefined in injected constructor

3 Answers 3

1

@Inject(...) in @Inject(Http) public _http: Http is redundant when the parameter to @Inject() is the same as the type of the parameter.

@Injectable()
export class HttpRest{
    groups;
    constructor(public _http: Http, public _ids: UserIds
    ){}
...
}

You need to provide Http and UserIds so DI is able to resolve the dependency. Http is included in HTTP_PROVIDERS

@Component({
  selector: '...',
  providers: [HTTP_PROVIDERS, UserIds],
  template: ...
})
export class AppComponent {

}

Ensure you have everything imported correctly

import {HTTP_PROVIDERS, Http} from 'angular2/http';
Sign up to request clarification or add additional context in comments.

3 Comments

The problem is actually that UserIds is undefined in the constructor params for some unknown reason that im trying to understand, so the title of this question becomes irrelevant. It should be "Imported service is undefined in constructors params". This happens even though if I save the imported Class in a member, I can see it assigned in the constructor execution context.
Do you have every class in its own file. If you use a class name that is in the same file further down it won't work. Classes are not hoisted. Otherwise an import is broken. Otherwise it would be helpful to have a Plunker that allows to reproduce (template plnkr.co/edit/KZu83k?p=info)
The class is in its own file that exports a single class. I will however create a plunker to show the problem, and post it as a new question. I will then update this post with a link to that question. For now I am adding an update comment to the main question here since the title for this problem is irrelevant.
1

Ok so I found the problem, and it has not to do with Inject. The problem was that im using an index file to export services, as mentioned in the angular 2 style guide (https://github.com/mgechev/angular2-style-guide/blob/master/old/README.md#directory-structure), and for some reason importing this specific service from the index causes an undefined value when injected into the constructor. When I reffernced the source directly and not trough the index file, for some reason that is unknown to, resolved the Error.

The import before the fix looked like this:

import {UserIds} from "../index"; 

Which worked with all other services and components.

The import using the direct source file:

import {UserIds} from "../user_ids/user_ids.service";

For some reason this solved my problem, but i want to stay consistent with the index.ts encapsulation.

If anyone has an idea why this could happen I'll be happy for updates.

1 Comment

I just ran into the same issue. Thanks for the solution but I'm also wondering what the problem is.. I don't want to reference my classes directly, but instead I'm building barrels....
0

You need to register your UserIds service at the root level (main.ts) or in the app.component.ts, which is the highest level in the hierarchy:

@Component({
    selector: 'my-app',
    template: '<h1>My First Angular 2 App</h1>',
    providers: [ UserIds ]
})
export class AppComponent { }

1 Comment

I did that without result. @Component({ selector:"...", templateUrl:"...", directives:[ ROUTER_DIRECTIVES ], providers:[ ROUTER_PROVIDERS, HttpRest, UserIds, provide(LocationStrategy, {useClass: HashLocationStrategy}) ] })

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.