Preloads modules on site component

Adds module preloading on site modules.
This commit is contained in:
Sean Engelhardt 2019-02-22 10:04:50 +01:00
parent f4f8b8422f
commit 3329932d27
3 changed files with 51 additions and 13 deletions

View File

@ -7,6 +7,7 @@ import { LoginLegalNoticeComponent } from './site/login/components/login-legal-n
import { LoginPrivacyPolicyComponent } from './site/login/components/login-privacy-policy/login-privacy-policy.component'; import { LoginPrivacyPolicyComponent } from './site/login/components/login-privacy-policy/login-privacy-policy.component';
import { ResetPasswordComponent } from './site/login/components/reset-password/reset-password.component'; import { ResetPasswordComponent } from './site/login/components/reset-password/reset-password.component';
import { ResetPasswordConfirmComponent } from './site/login/components/reset-password-confirm/reset-password-confirm.component'; import { ResetPasswordConfirmComponent } from './site/login/components/reset-password-confirm/reset-password-confirm.component';
import { AppPreloader } from './shared/utils/app-preloader';
/** /**
* Global app routing * Global app routing
@ -29,7 +30,8 @@ const routes: Routes = [
]; ];
@NgModule({ @NgModule({
imports: [RouterModule.forRoot(routes)], imports: [RouterModule.forRoot(routes, { preloadingStrategy: AppPreloader })],
exports: [RouterModule] exports: [RouterModule],
providers: [AppPreloader]
}) })
export class AppRoutingModule {} export class AppRoutingModule {}

View File

@ -0,0 +1,27 @@
import { PreloadingStrategy, Route } from '@angular/router';
import { Observable, of } from 'rxjs';
/**
* Custom preload strategy class
*
* @example
* ```
* {
* path: 'agenda',
* loadChildren: './agenda/agenda.module#AgendaModule',
* data: { preload: true }
* }
* ```
*/
export class AppPreloader implements PreloadingStrategy {
/**
* Custom preload function.
* Can be add to routes as data argument.
*
* @param route The route to load
* @param load The load function
*/
public preload(route: Route, load: Function): Observable<any> {
return route.data && route.data.preload ? load() : of(null);
}
}

View File

@ -2,7 +2,6 @@ import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router'; import { Routes, RouterModule } from '@angular/router';
import { SiteComponent } from './site.component'; import { SiteComponent } from './site.component';
import { AuthGuard } from '../core/core-services/auth-guard.service'; import { AuthGuard } from '../core/core-services/auth-guard.service';
/** /**
@ -17,43 +16,53 @@ const routes: Routes = [
children: [ children: [
{ {
path: '', path: '',
loadChildren: './common/common.module#CommonModule' loadChildren: './common/common.module#CommonModule',
data: { preload: true }
}, },
{ {
path: 'agenda', path: 'agenda',
loadChildren: './agenda/agenda.module#AgendaModule' loadChildren: './agenda/agenda.module#AgendaModule',
data: { preload: true }
}, },
{ {
path: 'assignments', path: 'assignments',
loadChildren: './assignments/assignments.module#AssignmentsModule' loadChildren: './assignments/assignments.module#AssignmentsModule',
data: { preload: true }
}, },
{ {
path: 'mediafiles', path: 'mediafiles',
loadChildren: './mediafiles/mediafiles.module#MediafilesModule' loadChildren: './mediafiles/mediafiles.module#MediafilesModule',
data: { preload: true }
}, },
{ {
path: 'motions', path: 'motions',
loadChildren: './motions/motions.module#MotionsModule' loadChildren: './motions/motions.module#MotionsModule',
data: { preload: true }
}, },
{ {
path: 'settings', path: 'settings',
loadChildren: './config/config.module#ConfigModule' loadChildren: './config/config.module#ConfigModule',
data: { preload: true }
}, },
{ {
path: 'users', path: 'users',
loadChildren: './users/users.module#UsersModule' loadChildren: './users/users.module#UsersModule',
data: { preload: true }
}, },
{ {
path: 'tags', path: 'tags',
loadChildren: './tags/tag.module#TagModule' loadChildren: './tags/tag.module#TagModule',
data: { preload: true }
}, },
{ {
path: 'history', path: 'history',
loadChildren: './history/history.module#HistoryModule' loadChildren: './history/history.module#HistoryModule',
data: { preload: true }
}, },
{ {
path: 'projectors', path: 'projectors',
loadChildren: './projector/projector.module#ProjectorModule' loadChildren: './projector/projector.module#ProjectorModule',
data: { preload: true }
} }
], ],
canActivateChild: [AuthGuard] canActivateChild: [AuthGuard]