@angular/router#PreloadingStrategy TypeScript Examples

The following examples show how to use @angular/router#PreloadingStrategy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: custom-preload-strategy.service.ts    From Angular-Cookbook with MIT License 6 votes vote down vote up
@Injectable({
  providedIn: 'root',
})
export class CustomPreloadStrategyService implements PreloadingStrategy {
  constructor() {}

  preload(route: Route, load: () => Observable<any>): Observable<any> {
    try {
      const { shouldPreload } = route.data;
      return shouldPreload
        ? this.loadRoute(route, load)
        : this.noPreload(route);
    } catch (e) {
      console.error(e);
      return this.noPreload(route);
    }
  }

  loadRoute(route: Route, loadFn: () => Observable<any>): Observable<any> {
    console.log(`Preloading done for route: ${route.path}`);
    return loadFn();
  }

  noPreload(route: Route): Observable<any> {
    console.log(`No preloading set for: ${route.path}`);
    return of(null);
  }
}