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