Answer the question
In order to leave comments, you need to log in
How to inject a dependency into an Angular 2 service?
Unable to inject angular http module into service. I'm running into this error
EXCEPTION: Can't resolve all parameters for UserService: (?)
//main.ts
import { bootstrap } from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';
import { HTTP_PROVIDERS } from '@angular/http';
bootstrap(AppComponent, [HTTP_PROVIDERS])
.catch(err => console.error(err));
//app.component.ts
import { Component } from '@angular/core';
import { MainMenuComponent } from './main-menu/main-menu.component';
@Component({
selector: 'edpt-app',
templateUrl: 'app/app.template.html',
directives: [MainMenuComponent]
})
export class AppComponent { }
//main-menu.component.ts
import { Component } from '@angular/core';
import { UserService } from '../security/user.service';
@Component({
selector: 'main-menu',
templateUrl: 'site/main-menu.template',
providers: [UserService]
})
export class MainMenuComponent {
constructor(private userService: UserService) {
}
}
//user.service.ts
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
@Injectable()
export class UserService {
private loggedIn;
constructor(private http: Http) {
}
}
Answer the question
In order to leave comments, you need to log in
It looks like you need to boot it up, it's like injected.
Try:
bootstrap(AppComponent, [HTTP_PROVIDERS, UserService ])
Typically, this error occurs when the order of imports / exports is violated, or something is not imported. If the type of the dependency injected into the constructor has not yet been loaded for some reason, then this error occurs. Moreover, from the point of view of typescript, everything is fine, because the compiler first of all looks at .d.ts files.
In this situation, most likely, the HTTP module from Angular is not correctly connected to the project. Those. The .d.ts files themselves are connected normally, but the js code itself is not included in the final assembly (or is included, but in the wrong order).
But you need to see the whole project to be more precise.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question