Pankaj Parkar
Indian | 30 | Ex MVP | Angular GDE
Senior Technology Consultant, Virtusa
It is technique of loading relevant resources on demand
pankajparkar
pankajparkar
App Module
Dashboard Module
Admin Module
Product Module
Roles Module
import
import
import
import
pankajparkar
const routes: Routes = [
{ path: 'dashboard', component: DashboardComponent },
{ path: 'roles', component: RolesComponent },
{ path: 'admin', component: AdminComponent },
{ path: 'product', component: ProductComponent },
{ path: '**', redirectTo: 'dashboard' },
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
@NgModule({
declarations: [
AppComponent
],
imports: [
...materialModules, BrowserAnimationsModule, BrowserModule,
AppRoutingModule, SharedModule,
// loading modules eagerly
AdminModule, DashboardModule, ProductModule, RolesModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
const routes: Routes = [
{
path: 'dashboard',
loadChildren: () => import('./dashboard/dashboard.module').then(m => m.DashboardModule)
},
{
path: 'roles',
loadChildren: () => import('./roles/roles.module').then(m => m.RolesModule)
},
{
path: 'admin',
loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule)
},
{
path: 'product',
loadChildren: () => import('./product/product.module').then(m => m.ProductModule)
},
{
path: '**',
redirectTo: 'dashboard'
},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
App Module
Dashboard Module
Admin Module
Product Module
Roles Module
pankajparkar
path: dashboard
path: admin
path: product
path: roles
import { PreloadingStrategy, Route } from '@angular/router';
import { Observable, of } from 'rxjs';
export class CustomPreloadStrategy implements PreloadingStrategy {
preload(route: Route, load: Function): Observable<any> {
return route.data && route.data.preload ? load() : of(null);
}
}
const routes: Routes = [
{
path: 'dashboard',
loadChildren: () => import('./dashboard/dashboard.module')
.then(m => m.DashboardModule)
},
{
path: 'roles',
loadChildren: () => import('./roles/roles.module').then(m => m.RolesModule),
data: { preload: true }, //<-- Added information here
},
...
];
@ViewChild('container', { read: ViewContainerRef }) container: any;
constructor(
) { }
async addBarComponent() {
this.container.clear();
const componentRef = this.container.createComponent(BarChartComponent);
}
clear() {
this.container.clear();
}
<button (click)="addBarComponent()">
Show Barchart Component
</button>
<ng-container #container></ng-container>
pankajparkar
pankajparkar
Source - https://github.com/pankajparkar/weather-cast
@Component({
selector: 'wc-weather-dashboard',
...
})
export class WeatherDashboardComponent {
@Input() headingStart = 'Weather Forecast in';
...
}
@NgModule({
declarations: [
WeatherDashboardComponent,
...
],
imports: [...],
exports: [WeatherDashboardComponent]
})
export class WeatherForecastModule { }
import './weather-forecast.module';
export * from './weather-dashboard/weather-dashboard.component';
@ViewChild('container', { read: ViewContainerRef }) container: any;
constructor(
private factory: ComponentFactoryResolver
) { }
async loadWeatherWidget() {
this.container.clear();
const weatherComponent = await import('../weather-forecast')
.then(i => i.WeatherDashboardComponent);
const componentRef = this.container.createComponent(weatherForecastWidget);
componentRef.instance.headingStart = 'Just for fun';
}
<button (click)="loadWeatherWidget()">
Load Weather Widget
</button>
<ng-container #container></ng-container>
pankajparkar
App Module
Dashboard Module
Admin Module
Product Module
Roles Module
pankajparkar
Source - https://github.com/angular/angular/discussions/43784
pankajparkar
@Component({
standalone: true,
imports: [
// Modules
...matModules,
CommonModule,
BrowserAnimationsModule,
ReactiveFormsModule,
FormsModule,
// Other Standalone Components
FiltersComponent,
WeatherForecastComponent,
WeatherForecastDetailsComponent,
WeatherForecastCityComponent,
WeatherForecastHistoryComponent,
],
selector: 'wc-weather-dashboard',
templateUrl: './weather-dashboard.component.html',
styleUrls: ['./weather-dashboard.component.css']
})
export class WeatherDashboardComponent implements OnInit, OnDestroy {
...
}
pankajparkar
RouterModule.forRoot([
{
path: 'weather-forecast',
loadComponent: () => import('../weather-component')
.then(m => m.WeatherDashboardComponent)
},
{
path: '/some/route/to/standalone/with/default/export',
loadComponent: () => import('./default-standalone.cmp')
}
]);
pankajparkar
pankajparkar
pankajparkar
pankajparkar
Emulating tree-shakable components using single component Angular modules
https://coryrylan.com/blog/custom-preloading-and-lazy-loading-strategies-with-angular
Weather App - https://github.com/pankajparkar/weather-cast
By Pankaj Parkar
lazy-loading-recipes