Angular2 httpはObservablesを返すので、すべてが非常に簡単になります。
観察可能でついに方法を持っている、あなたはその内削除コードを置くことができます。
以下のコードは簡単な実装です。
document.querySelector('body').classList.add('loading');
this._http.get(url)
.finally(() => {
document.querySelector('body').classList.remove('loading');
})
.subscribe(
res => { console.log('success result --', res); },
err => { console.log('error', err); },
() => { console.log('completed'); }
)
すべてのhttp getコールで共通に実装したい場合は、
下記のようにCustomHttp
を作成して、http
の代わりにCustomHttp
を使用してください。
@Injectable()
export class CustomHttp {
constructor(private _http: Http) {
}
get(url: string, options?) {
document.querySelector('body').classList.add('loading');
return this._http.get(url, options).finally(() => {
document.querySelector('body').classList.remove('loading');
});
}
}
ありがとうございます。ng2のおかげで、私たちはHttpクラスを拡張する必要があります。私はそれを調べます。ヒントのおかげで。 – chungtinhlakho