Novice to Angular2 and running into strange issue. When rendering index.html the app.component renders the html part OK, but it fails on data call in user.service.t
In your app.module.ts, import UserService like this-
import { UserService } from './services/user.service';
Provide it in NgModule like this-
@NgModule({
imports: [BrowserModule,HttpModule],
declarations: [ AppComponent ],
providers: [ UserService ],
bootstrap: [ AppComponent ]
})
See if this helps.
You need to provide the user service in your app.module and remove the @Inject
and the providers
entry in the app.component
:
app.module.ts
import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
//Add your UserService import here
@NgModule({
imports: [
BrowserModule,
HttpModule],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ],
providers: [ UserService ]
})
export class AppModule { }
app.component.ts
import { Component, OnInit, Inject } from '@angular/core';
import { UserService } from './services/user.service';
import { User } from './models/user';
@Component({
selector: 'my-app',
templateUrl: 'app/app.component.html'
})
export class AppComponent implements OnInit {
user: User;
constructor(
private _userService: UserService) {
}
getCurrentUser() {
this._userService.getCurrentUser()
.subscribe(user => this.user = user);
}
ngOnInit() {
this.getCurrentUser();
}
}
this way, UserService
dependencies are managed by the injector and you don't provide the service directly to your component (this behaviour is deprecated or even removed, don't know since final release).