File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed
Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ import { PreloadingStrategy , Route } from '@angular/router' ;
2+ import { Injectable } from '@angular/core' ;
3+ import { Observable , Subject } from 'rxjs' ;
4+ import { switchMap } from 'rxjs/operators' ;
5+
6+ @Injectable ( { providedIn : 'root' } )
7+ export class CustomPreloadingStrategy implements PreloadingStrategy {
8+ private preloadTriggers : { [ key : string ] : Subject < void > } = { } ;
9+
10+ preload ( route : Route , load : ( ) => Observable < any > ) : Observable < any > {
11+ console . log ( 'CustomPreloadingStrategy: Checking route' , route . data , route . path ) ;
12+ if ( route . data && route . data [ 'preloadOnDemand' ] ) {
13+ if ( ! this . preloadTriggers [ route . path ! ] ) {
14+ this . preloadTriggers [ route . path ! ] = new Subject < void > ( ) ;
15+ }
16+ return this . preloadTriggers [ route . path ! ] . asObservable ( ) . pipe (
17+ // When triggered, start loading
18+ switchMap ( ( ) => load ( ) )
19+ ) ;
20+ }
21+ // Default: do not preload
22+ return new Observable < any > ( ) ;
23+ }
24+
25+ triggerPreload ( path : string ) {
26+ if ( this . preloadTriggers [ path ] ) {
27+ this . preloadTriggers [ path ] . next ( ) ;
28+ }
29+ }
30+ }
Original file line number Diff line number Diff line change 1+ import { Injectable } from '@angular/core' ;
2+ import { CustomPreloadingStrategy } from './custom-preloading.strategy' ;
3+
4+ @Injectable ( { providedIn : 'root' } )
5+ export class PreloadService {
6+ constructor ( private strategy : CustomPreloadingStrategy ) { }
7+
8+ preload ( path : string ) {
9+ this . strategy . triggerPreload ( path ) ;
10+ }
11+ }
You can’t perform that action at this time.
0 commit comments