Skip to content

Commit 5cc3a7f

Browse files
committed
feat(lazy-loading): add lazy demo and fixed HMR
closes #191 updates #165 (still needs to have speed improved)
1 parent ad05cbc commit 5cc3a7f

File tree

5 files changed

+50
-15
lines changed

5 files changed

+50
-15
lines changed

Client/app/app.module.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function createTranslateLoader(http: Http, baseHref) {
4242
HomeComponent,
4343
ChatComponent,
4444
Ng2BootstrapComponent
45-
],
45+
],
4646
imports: [
4747
CommonModule,
4848
HttpModule,
@@ -131,6 +131,8 @@ export function createTranslateLoader(http: Http, baseHref) {
131131
}
132132
},
133133

134+
{ path: 'lazy', loadChildren: './containers/+lazy/lazy.module#LazyModule'},
135+
134136
// All else fails - go home!
135137
{ path: '**', redirectTo: 'home' }
136138
])

Client/app/components/navmenu/navmenu.component.html

+7-2
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@
2424
</li>
2525
<li [routerLinkActive]="['link-active']">
2626
<a [routerLink]="['/users']">
27-
<span class='glyphicon glyphicon-user'></span> REST API Demo
27+
<span class='glyphicon glyphicon-user'></span> Rest API Demo
2828
</a>
2929
</li>
3030
<li [routerLinkActive]="['link-active']">
3131
<a [routerLink]="['/ng2-bootstrap']">
32-
<span class='glyphicon glyphicon-user'></span> ng2-Bootstrap demo
32+
<span class='glyphicon glyphicon-th-large'></span> ngx-Bootstrap demo
33+
</a>
34+
</li>
35+
<li [routerLinkActive]="['link-active']">
36+
<a [routerLink]="['/lazy']">
37+
<span class='glyphicon glyphicon-star-empty'></span> Lazy-loaded demo
3338
</a>
3439
</li>
3540
<!--<li [routerLinkActive]="['link-active']">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Component } from '@angular/core';
2+
3+
@Component({
4+
selector: 'lazy-view',
5+
template: `
6+
<h1>Lazy-Loaded Component!</h1>
7+
<blockquote>
8+
Fun fact: This was lazy-loaded :)
9+
Check your Network tab!
10+
</blockquote>
11+
`
12+
})
13+
export class LazyComponent { }
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { NgModule, Component } from '@angular/core';
2+
import { RouterModule } from '@angular/router';
3+
import { LazyComponent } from './lazy.component';
4+
5+
@NgModule({
6+
declarations: [LazyComponent],
7+
imports: [
8+
RouterModule.forChild([
9+
{ path: '', component: LazyComponent, pathMatch: 'full' }
10+
])
11+
]
12+
})
13+
export class LazyModule {
14+
15+
}

Client/main.browser.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ import { BrowserAppModule } from './app/browser-app.module';
66
const rootElemTagName = 'app'; // Update this if you change your root component selector
77

88
// // Enable either Hot Module Reloading or production mode
9-
// if (module['hot']) {
10-
// module['hot'].accept();
11-
// module['hot'].dispose(() => {
12-
// // Before restarting the app, we create a new root element and dispose the old one
13-
// const oldRootElem = document.querySelector(rootElemTagName);
14-
// const newRootElem = document.createElement(rootElemTagName);
15-
// oldRootElem.parentNode.insertBefore(newRootElem, oldRootElem);
16-
// modulePromise.then(appModule => appModule.destroy());
17-
// });
18-
// } else {
19-
// enableProdMode();
20-
// }
9+
if (module['hot']) {
10+
module['hot'].accept();
11+
module['hot'].dispose(() => {
12+
// Before restarting the app, we create a new root element and dispose the old one
13+
const oldRootElem = document.querySelector(rootElemTagName);
14+
const newRootElem = document.createElement(rootElemTagName);
15+
oldRootElem.parentNode.insertBefore(newRootElem, oldRootElem);
16+
modulePromise.then(appModule => appModule.destroy());
17+
});
18+
} else {
19+
enableProdMode();
20+
}
2121

2222
const modulePromise = platformBrowserDynamic().bootstrapModule(BrowserAppModule);

0 commit comments

Comments
 (0)