All Projects → Quramy → ng2-lazy-load-demo

Quramy / ng2-lazy-load-demo

Licence: other
A sample repository for Angular2 lazy module loading

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to ng2-lazy-load-demo

amod
Autonomous Vehicles Intelligence Analysis
Stars: ✭ 36 (+63.64%)
Mutual labels:  demonstration
f1usb
Einfaches Beispiel für die Entwicklung eines USB FullSpeed Device auf Basis des STM32F103 ohne die Verwendung externer Bibliotheken
Stars: ✭ 27 (+22.73%)
Mutual labels:  demonstration
Gans Awesome Applications
Curated list of awesome GAN applications and demo
Stars: ✭ 3,933 (+17777.27%)
Mutual labels:  demonstration
svd-image-compression-demo
Demonstration of low rank matrix approximations via singular value decomposition
Stars: ✭ 23 (+4.55%)
Mutual labels:  demonstration
flexible-fn-rs
Demonstration of flexible function calls in Rust with function overloading, named arguments and optional arguments
Stars: ✭ 80 (+263.64%)
Mutual labels:  demonstration
getplay
The simplest way to integrate vue-play into your project
Stars: ✭ 13 (-40.91%)
Mutual labels:  demonstration

Angular2 Lazy Module Loading Demo

It's a sample repository to explain how to lazy module loading in your Angular2 App with webpack2.

This repository includes JiT(Just in Time) and AoT(Ahead of Time) demos. The AoT demo uses ngc(angular/compiler-cli).

Demonstration

https://quramy.github.io/ng2-lazy-load-demo/index.html

Build

To build this in your local machine, clone this repository and exec the following:

npm i
npm run build
npm start

How to implement async sub module loading?

In Angular2 router, to load sub modules asynchronously, you can use loadChildren.

The following code explains the steps to achieve AoT lazy loading:

import { Routes, RouterModule } from "@angular/router";
import { MainHomeComponent } from "./main-home.component";
import { MainAboutComponent } from "./main-about.component";

// import { SubAppComponent } from "../sub/sub-app.component";
// import { SubHomeComponent } from "../sub/sub-home.component";
// import { SubModule } from "../sub/sub.module";

export const appRoutes: Routes = [
  {path: "", component: MainHomeComponent},
  {path: "about", component: MainAboutComponent },

  /* 1. Simple nested routing: */
  // The following configuration allows to nested routing.
  // But the sub components are included bundle.js so the browser loads them on init.
  //
  // {
  //   path: "sub",
  //   component: SubAppComponent,
  //   children: [
  //     {path: "", component: SubHomeComponent}
  //   ]
  // },
  //

  /* 2. Separate sub modules and use load children callback function: */
  // See the loadSubModule function in this code.
  // {path: "sub", loadChildren: loadSubModule},

  /* 3. Auto switching module or moduleFactory with angular2-load-children-loader */
  // See the loader section of webpack.config.js .
  {path: "sub", loadChildren: "es6-promise-loader?,[name]!../sub/sub.module#SubModule" },

];

// export function loadSubModule(): any {
//   // 2-1 Naive loading sub module:
//   // It's synchronous loading
//   // return SubModule;
//
//   // 2-2 Async module load with es6-promise-loader:
//   // You can create submodule's chunk with webpack es6-promise-loader.
//   // However you should switch the module to load with the context:
//   // * JiT:
//   // return require("es6-promise-loader!../sub/sub.module")("SubModule");
//   // * AoT:
//   // return require("es6-promise-loader!../sub/sub.module.ngfactory")("SubModuleNgFactory");
// }

export const routing = RouterModule.forRoot(appRoutes, { useHash: true});

And the following part of webpack.config.js is important:

/* webpack.config.js */
// ...
    module: {
      rules: [
        {
          test: /\.ts$/,
          loader: [
            "ts-loader",
            "angular2-load-children-loader" // this loader replace loadChildren value to function to call require.
          ],
        }
      ],
      noParse: [
        /zone\.js\/dist/,
      ]
    },

// ...

  output: {
    path: path.resolve(__dirname, "dist"),
    publicPath: "http://localhost:3000/dist/",
    filename: "[name].js",
    chunkFilename: "[name].chunk.js", // the chunk files are created by es6-promise-loader.
  },

// ...

Please see also https://github.com/Quramy/angular2-load-children-loader.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].