我正在尝试按照MEAN堆栈教程构建一个todo-list,我遇到问题我的组件,我注入我的服务不起作用,我没有任何错误消息。有什么想法吗?
task.service.ts
import {Injectable} from '@angular/core';
import {Http, Headers} from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class TaskService{
constructor(private http:Http){
console.log('Task service initialized...'); //this part works
}
getTasks(){
return this.http.get('http://localhost:3000/api/tasks')
.map(res => res.json());
}
}
tasks.component.ts
import { Component } from '@angular/core';
import {TaskService} from '../../services/task.service';
@Component({
moduleId: module.id,
selector: 'tasks',
templateUrl: 'tasks.component.html',
})
export class TasksComponent {
constructor(private taskService:TaskService){
this.taskService.getTasks()
.subscribe(tasks => {
console.log(tasks);
});
}
}
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { TasksComponent } from './components/tasks/tasks.component';
@NgModule({
imports: [ BrowserModule, HttpModule ],
declarations: [ AppComponent, TasksComponent ],
bootstrap: [ AppComponent ],
})
export class AppModule { }
app.component.ts
import { Component } from '@angular/core';
import {TaskService} from './services/task.service';
@Component({
moduleId: module.id,
selector: 'my-app',
templateUrl: 'app.component.html',
providers: [TaskService]
})
export class AppComponent { }