0
我是新來的打字稿,我下面的教程Tour of heroes from angular page在本教程的最後一章時,我嘗試使用HTTP與下面的代碼打字稿無效左手操作
一切都正常運行,雖然我得到這個錯誤來自打字稿編譯器。
的問題是,因爲這個錯誤我必須刷新自browsersync因爲錯誤是不同步的瀏覽器。
身體能幫助我嗎?
以下是代碼
Hero.service.ts
import { HEROES }from './mock.heroes';
import { Hero } from './hero';
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class HeroService {
url: string;
constructor(private http: Http){
this.url = "http://jsonplaceholder.typicode.com/users";
this.http = http;
}
getHeros(): Promise<Hero[]> {
if(!HEROES || HEROES.length == 0) {
return this.http.get(this.url)
.toPromise()
.then(response => {
HEROES = response.json();
});
} else {
let promise: Promise<Hero[]> = new Promise<Hero[]>(function(resolve, reject){
resolve(HEROES);
});
return promise;
}
}
}
mock.heroes.ts
import { Hero } from './hero';
export var HEROES: Hero[];
感謝abhi它幫助我循環了json數組。雖然response.json()返回一個javascript對象數組而不是json數組,但我無法直接將它轉換爲Hero [] –
高興地幫助:),您可以標記答案已被接受 – abhirathore2006