[英]how to fetch data from service in angular 6?
我正在為我的演示應用程序使用此api( https://restcountries.eu/ )
API網址是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'
})
});
}
使用angular cli創建項目時看到錯誤
只需刪除:
headers :new HttpHeaders({
'content-type': 'application/json'
})
最終代碼:
fetchDataThroughPromise() {
return this.http.get('https://restcountries.eu/rest/v2/all')
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.