我收到來自http服務的響應對象,角度爲2,如果我嘗試控制檯response.prop,它不工作,它顯示以下錯誤:dot(例如.reponse.title)屬性無法處理從角度2中的http服務獲得的響應對象
D:/ABC/angular-training/angular_cli/src/app/shared/footer/footer.component.ts (16,28): Property 'prop' does not exist on type '{}'.)
但是當我做響應[「託」]然後我得到我的價值。
這裏是我的代碼:
export class FooterComponent {
constructor(private httpService : HttpService) {
this.httpService.getData("http://127.0.0.1:8887/footer.json").then(response => {
console.log(response);//shows object
console.log(response.prop);//not works
console.log(response["prop"]);//works fine
});
}
}
這裏是我的HTTPService代碼:
export class HttpService {
private Title = URL;// URL to web api
constructor(private http: Http) { }
getData(url="this.Title"): Promise<{}> {
return this.http.get(url)
.toPromise()
.then(response =>
response.json()
)
.catch(this.handleError);
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
}
而且這是從服務器的JSON響應:
{
"footerLinks" : {
"social":[{
"text":"Google",
"link":"www.google.com"
},
{
"text":"Facebook",
"link":"www.facebook.com"
},
{
"text":"Twitter",
"link":"www.Twiiter.com"
},
{
"text":"Pinterest",
"link":"www.Pinterest.com"
},
{
"text":"LinkedIn",
"link":"www.linkedin.com"
}]
}
}
你的回答是怎樣的?這將有助於說明爲什麼它不能正常工作:) – Alex