写好四个“这个”就能获取数据

    1. //app.module.ts中去注册
    2. import {HttpClientModule} from '@angular/common/http'//这个
    3. @NgModule({
    4. ...
    5. imports: [
    6. BrowserModule,
    7. HttpClientModule//这个
    8. ]
    9. ...
    10. })
    1. //app.component.ts配置
    2. import {HttpClient} from '@angular/common/http'//这个
    3. export class AppComponent {
    4. constructor(public http:HttpClient){}//这个
    5. ngOnInit(){
    6. var url:string = "http://localhost:8000/top250";
    7. this.http.get(url).subscribe(res=>{
    8. console.log(res)
    9. })
    10. }
    11. }