我想要做的是:
我希望在http請求發生時使用微調器。換句話說,我希望用戶在我的app.component中發生http請求時看到加載屏幕。
我的spinner.component和微調服務文件與this問題中的答案相同。
而我app.component的成分是每個http請求上的角度2加載器
@Component({
selector: 'todoApi',
template: `
<div class="foo">
<spinner-component></spinner-component>
<h1>Internship Project</h1>
<a [routerLink]="['Dashboard']">Dashboard</a>
<a [routerLink]="['Tasks']">List</a>
<router-outlet></router-outlet>
<div>
`,
directives: [ROUTER_DIRECTIVES,SpinnerComponent],
providers: [
ROUTER_PROVIDERS,
]
})
@RouteConfig([
{
path: '/dashboard',
name: 'Dashboard',
component: DashboardComponent,
useAsDefault: true
},{
path: '/tasks',
name: 'Tasks',
component: TaskComponent
},{
path: '/detail/:id',
name: 'TaskDetail',
component: TaskDetailComponent
},
])
要conclue,每當一個HTTP請求在這些線路之一發生時,我想表明的微調器用戶。我知道這是一個不好的問題,但我是角2的新手,如果有人能幫助我,我會非常感激。
非常感謝!
編輯!:
解決方案與半滑舌鰨的回答是:我 我的包裹和http
到spinner-service
一個HttpClient
成分,並用它來代替普通的HTTP模塊。這裏是我的HttpClient
組件:
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { SpinnerService} from './spinner-service';
@Injectable()
export class HttpClient {
constructor(
private http: Http,
public spinner: SpinnerService
){
}
createAuthorizationHeader(headers:Headers) {
headers.append('Authorization', 'Basic ' + btoa('username:password'));
}
get(url) {
this.spinner.start();
let headers = new Headers();
this.createAuthorizationHeader(headers);
return this.http.get(url, { headers: headers }).do(data=> {this.spinner.stop()});
}
post(url, data) {
this.spinner.start();
let headers = new Headers();
this.createAuthorizationHeader(headers);
return this.http.post(url, data, { headers: headers }).do(data=> {this.spinner.stop()});
}
}
也許你創建定製服務,進行增由角提供的HttpClass HTTP請求,並跟蹤在您的自定義服務的'isBusy'狀態每個請求。 – Lekhnath