Angular4+ 頁面切換 顯示進度條

  使用ngx-progressbar 插件能夠經過簡單幾句代碼實現loading加載效果。npm

  npm install ngx-progressbar --savejson

  在app.module.ts文件中,咱們須要引用NgProgressModule,而後添加到NgModule的imports中。在公共頁面添加標籤ng-progress,默認效果是紅色。想要修改顯示特效。能夠查看https://www.npmjs.com/package/ngx-progressbar#ngprogresscomponent-options-inputs 。bootstrap

  全局加載動做條代碼:swift

  

 1 ...
 2 import { NgProgressModule, NgProgressBrowserXhr } from 'ngx-progressbar';
 3 import { BrowserXhr } from '@angular/http';
 4 ...
 5 
 6 @NgModule({
 7   declarations: [
 8     AppComponent
 9   ],
10   imports: [
11     BrowserModule,
12     NgProgressModule,
13     HttpModule
14   ],
15   providers: [
16     {provide: BrowserXhr, useClass: NgProgressBrowserXhr}
17   ],
18   bootstrap: [AppComponent]
19 })
20 export class AppModule { }

 

  而咱們想要在單個接口中使用加載動做條。須要在http方法中使用progressService的方法:app

ngOnInit() {
    const sampleUrl = 'http://bla.com';

    this.progressService.start();
    setTimeout(() => {
      this.progressService.set(0.1);
    }, 1000);
    setTimeout(() => {
      this.progressService.inc(0.2);
    }, 2000);
    this.http.get(sampleUrl)
      .subscribe((response) => {
        this.progressService.done();
        this.posts = response.json();
      });
}

  這樣加載接口時,頁面頂部會顯示加載動畫。ide

相關文章
相關標籤/搜索