显示每个http请求angular2的加载栏

时间:2017-05-15 10:23:05

标签: angular progress-bar loading angular-http-interceptors

我正在将我的应用程序从angularjs移动到angular2。我使用的是Angular Loading Bar

  

使用角度拦截器的自动装载杆。有用   自动,所以简单地将它作为依赖包括它,它会   自动显示$ http请求的进度。

More info

我正在尝试在angular2中找到类似的插件。我遇到了一些像ng2-slim-loading-bar这样的人,但在这里我必须手动管理进度。对于每个http请求,我必须手动启动进度条,然后完成它。

那么,是否有任何可用的插件可以完成Angular Loading Bar在angularjs中的作用。或者我如何修改任何现有的插件,使其表现得像这样。

2 个答案:

答案 0 :(得分:12)

您可以使用ngx-progressbar。在HTTP请求运行时,它可以automatically显示进度条。

您所要做的就是:

1-导入并提供NgProgressCustomBrowserXhr

import { NgProgressCustomBrowserXhr } from 'ngx-progressbar';

@NgModule({
 providers: [
   // ...
   { provide: BrowserXhr, useClass: NgProgressCustomBrowserXhr } ,
 ],
 imports: [
   // ...
   NgProgressModule
 ]
})

2-在模板中使用如下所示。

<ng-progress></ng-progress>
  

您的HTTP将自动开始并完成进度   要求。无需使用NgProgressService来调用start()/ done()   手动

答案 1 :(得分:1)

以下是我在项目中所做的工作(Red Hat Automated Migration Toolkit):

  • 在我们的HTTP服务包装器(处理OAuth)中,我们触发一个事件,
  • 然后由我们LoadingIndicatorService抓住。
  • LoadingIndicatorService
    • 包裹SlimLoaderBarService
    • 并跟踪正在进行的HTTP请求数量。
    • 然后它计算百分比并将其放在20%到90%的范围内。
    • 当所有HTTP请求完成后,它会保持90%大约一秒钟,然后调用complete()

如果您对每个导航步骤有多个请求,这看起来很自然,并提供了良好的用户体验。 如果您通常只有1个请求,那么您可能想要调整基于CSS的动画(使其更长)或者使用start() afterall。

以下是一些关键代码部分:

@Injectable()
export class LoadingIndicatorService {

    constructor(
        private _slimBarService: SlimLoadingBarService,
        private _eventBusService: EventBusService,
    ) {
        // Register the LoadingSomething event listeners.
        this._eventBusService.onEvent
            .filter(event => event.isTypeOf(LoadingSomethingStartedEvent))
            .subscribe((event: LoadingSomethingStartedEvent) => this.loadingStarted() )
        this._eventBusService.onEvent
            .filter(event => event.isTypeOf(LoadingSomethingFinishedEvent))
            .subscribe((event: LoadingSomethingFinishedEvent) => this.loadingFinished() )
    }

    public getSlimService(){
        return this._slimBarService;
    }


    private counter: number = 0;
    private max: number = void 0;

    private reset() {
        this.counter = 0;
        this.max = void 0;
    }

    public loadingStarted(){
        this.counter++;
        this.max = this.counter;
        this.updateProgress();
    }

    public loadingFinished(){
        this.counter--;
        this.updateProgress();
    }

    private updateProgress() {
        if (this.counter == 0) {
            this._slimBarService.height = "2px";
            this._slimBarService.visible = true;
            this._slimBarService.progress = 95;
            this.max = void 0;
            Observable.timer(700).subscribe(() => {
                this._slimBarService.complete();
            });
        }
        else {
            // max - counter = finished.
            // If the things to load are added after something loaded, the progress would go back.
            // But let's rely on that loading will start fast at the beginning.
            // Start at 20, jump to 90.
            let percent = 20 + 70 * (1 - (this.max - this.counter) / this.max);
            this._slimBarService.height = "3px";
            this._slimBarService.color = "#39a5dc";
            this._slimBarService.visible = true;
            this._slimBarService.progress = percent;
        }
    }

}


    let responseObservable2 = responseObservable.do(
        () => console.log("Request SUCCEEDED"),
        () => console.log("Request FAILED"),
        () => {
            console.log("Request FINISHED");
            if (this._eventBus) {
                console.log("Request FINISHED, firing");
                this._eventBus.fireEvent(new LoadingSomethingFinishedEvent(responseObservable))
            }
        }
    );

HTTP服务包装器:

@Injectable()
export class WindupHttpService extends Http {

    private configureRequest(method: RequestMethod, f: Function, url: string | Request, options: RequestOptionsArgs = {}, body?: any): Observable<Response> {
        let responseObservable: Observable<Response> = ...

    ...

    console.log("Load STARTED");
    if (this._eventBus)
        console.log("Load STARTED, firing");
    this._eventBus.fireEvent(new LoadingSomethingStartedEvent(responseObservable));

    return responseObservable2;
}

有关完整代码,请搜索github.com以获取项目Windup。