Ad
How To Show Loading Component During Lazy Loading
I did lazy loading config in my angular project. That is done, But there is a problem.
because of my lazy loading component is too big (700kb), time to load component from web server is too long (0.5s)
I have to show loading component when lazy loading.
But I can not find loading option in angular router module.
I try to find angular router type definition about lazy load.
const routes: Routes = [
{ path: '', component: RandingPageComponent },
{
path: 'teams/:name',
// Loading component cannot here
loadChildren: 'src/app/domain/domain.module#DomainModule'
},
{ path: '**', component: NotfoundComponent }
];
Ad
Answer
try this (app.component.ts)
import { Component } from "@angular/core";
import { Router, NavigationStart, NavigationEnd, Event, NavigationCancel,
NavigationError } from "@angular/router";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.scss"]
})
export class AppComponent {
public showLoadingIndicator: boolean = true;
constructor(private _router: Router) {
this._router.events.subscribe((routerEvent: Event) => {
if (routerEvent instanceof NavigationStart) {
this.showLoadingIndicator = true;
}
if (routerEvent instanceof NavigationEnd ||
routerEvent instanceof NavigationCancel ||
routerEvent instanceof NavigationError) {
this.showLoadingIndicator = false;
}
});
}
}
in html (app.component.ts)
<div *ngIf="showLoadingIndicator" class="loading">Loading…</div>
<router-outlet></router-outlet>
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad