Show activity Indicator while loading a lazy loaded Module in Angular 2

前端 未结 5 1019
后悔当初
后悔当初 2020-12-13 04:06

My scenario is as follows. I have a menu, with multiple options. Each menu should be shown depending on user permissions (already solved), most menu items are encapsulated a

相关标签:
5条回答
  • 2020-12-13 04:35

    you can do it like this

    1. in app.component.html
    <div class="main-loader" *ngIf="loading">
      <div class="cssload-container" >
          <div class="cssload-whirlpool"></div>
      </div>
    </div>
    
    1. in app.component.ts

      import { Router, NavigationStart, NavigationEnd } from '@angular/router';
      
      
      loading:boolean = false;
      constructor(private router:Router) { 
        router.events.subscribe(event => {
          if(event instanceof NavigationStart) {
            this.loading = true;
            console.log("event started")
          }else if(event instanceof NavigationEnd) {
            this.loading = false;
            console.log("event end")
          }
          // NavigationEnd
          // NavigationCancel
          // NavigationError
          // RoutesRecognized
        });
      
      }
      
    2. in css any loading animation

    hope this is useful to you. thanks

    0 讨论(0)
  • 2020-12-13 04:40

    You can just use CSS !

    <routler-outlet></routler-outlet>
    <div class='.loader>
      Just, wait a sec ! I'm loading
    </div>
    

    In your template

    router-outlet + .loader {
      opacity : 1;
    }
    
    .loader {
      opacity : 0;
    }
    

    Then you can create fancy spinners with HTML/CSS

    0 讨论(0)
  • 2020-12-13 04:44

    You can listen for two router events:

    • RouteConfigLoadStart
    • RouteConfigLoadEnd

    They fire when a lazy loaded module is being loaded. The advantage of using these over the standard router events such as NavigationStart is that they won't fire on every route change.

    Listen to them in your root AppComponent to show / hide your spinner.

    app.component.ts

    import { Router, RouteConfigLoadStart, RouteConfigLoadEnd } from '@angular/router';
    
    ...
    
    export class AppComponent implements OnInit {
    
        loadingRouteConfig: boolean;
    
        constructor (private router: Router) {}
    
        ngOnInit () {
            this.router.events.subscribe(event => {
                if (event instanceof RouteConfigLoadStart) {
                    this.loadingRouteConfig = true;
                } else if (event instanceof RouteConfigLoadEnd) {
                    this.loadingRouteConfig = false;
                }
            });
        }
    }
    

    app.component.html

    Just a simple string here, but you could use a spinner component.

    <router-outlet></router-outlet>
    
    <ng-container *ngIf="loadingRouteConfig">Loading route config...</ng-container>
    

    I'm using this approach with Angular v4.2.3

    0 讨论(0)
  • 2020-12-13 04:50

    It's possible to click a link to other lazy-loaded route while loading first one. That's why we need to count routes being loaded:

    app.component.ts

    ```

    export class AppComponent { 
    
      currentlyLoadingCount = this._router.events.scan((c, e) => this._countLoads(c, e), 0);
    
      constructor(private _router: Router) { }
    
      private _countLoads(counter: number, event: any): number {
        if (event instanceof RouteConfigLoadStart) return counter + 1;
        if (event instanceof RouteConfigLoadEnd) return counter - 1;
        return counter;
      }
    

    ```

    app.component.html <ng-container *ngIf="currentlyLoadingCount | async">Loading route config...</ng-container>

    0 讨论(0)
  • 2020-12-13 04:55

    for those who need compatibility (and deal) with IE11, the accepted answer is not working, cause IE11 doesn't render the loader when switching from one module to another, so I did a (not very elegant but working) workaround:

    In my menuItem switch click event:

      public navigate(url: string) {
        this.configurationService.loading = true; //service variable linked with html loader
    
        //let's give time to tortoise IE11 to render loader before navigation...
        let obj = this;
        setTimeout(function() 
        {
          obj.router.navigateByUrl(url);
        }, 1);
      }
    
    0 讨论(0)
提交回复
热议问题