学习本教程时遇到错误:https://metanit.com/web/angular2/6.1.php
错误:(类型'HttpService'上不存在属性'getData')
服务文件:
import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
@Injectable()
export class HttpService{
constructor(private http: Http){ }
getData(){
return this.http.get('user.json')
}
}
app模块
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
组件文件:
import { Component, OnInit } from '@angular/core';
import { Response} from '@angular/http';
import { HttpService} from './http.service';
import {User} from './user';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers:[HttpService]
})
export class AppComponent implements OnInit{
title = 'app works!';
user: User;
constructor(private httpService: HttpService){}
ngOnInit(){
this.httpService.getData().subscribe((data: Response) => this.user=data.json());
}
}