如何从角度6的服务中获取数据?

时间:2018-07-08 06:44:14

标签: javascript angular

我正在为我的演示应用程序(https://restcountries.eu/)使用此api

API url是这个 https://restcountries.eu/rest/v2/all

但是我不能从服务中以角度获取数据

这是我的代码 https://stackblitz.com/edit/angular-3jpqrc?file=src%2Fapp%2Fapp.component.ts

constructor(private dataservice : DataService){

     this.dataservice.fetchDataThroughPromise().subscribe(()=>{
      console.log('s')
    })
  } 

服务代码

 fetchDataThroughPromise(){
    return this.http.get('https://restcountries.eu/rest/v2/all',
      {
        headers :new HttpHeaders({
          'content-type': 'application/json'
        })
      });
  }

使用角度cli创建项目时查看错误

enter image description here

1 个答案:

答案 0 :(得分:1)

只需删除:

    headers :new HttpHeaders({
      'content-type': 'application/json'
    })

最终代码:

fetchDataThroughPromise() {
  return this.http.get('https://restcountries.eu/rest/v2/all')
}