想要读取 json 文件并构建 settingsProvider。
所以我这样做:
import {Http} from "angular2/http";
import {Injectable} from "angular2/core";
@Injectable()
export class SettingsProvider{
url: string = "";
constructor(http: Http){
http.request('js/app/config/config.json').map(data => data.json()).subscribe(data => this.url = data.url);
}
getUrl():string {
return this.url;
}
}
但是遇到了这样的错误:
所以,我的第一个问题是——为什么会这样?
第二个问题:
当我这样做时:
http.request('js/app/config/config.json').subscribe(data => {
this.url = data.json().url;
});
this指向的不是类,而是订阅者实例。为什么这样?我认为 TypeScript 中的“fat-arrow”-lambda 可以帮助我们摆脱这种奇怪的闭包。