【发布时间】:2016-12-28 05:27:05
【问题描述】:
我是 typescript 的新手,我正在关注教程 Tour of heroes from angular page 在教程的最后一章中,当我尝试通过以下代码使用 HTTP 时
尽管我从 typescript 编译器收到此错误,但一切正常。
问题是因为这个错误,我必须刷新浏览器,因为 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[];
【问题讨论】:
标签: javascript typescript browser-sync