Giter VIP home page Giter VIP logo

ng2-lazy-load-demo's Introduction

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?,[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!../sub/sub.module")("SubModule");
//   // * AoT:
//   // return require("es6-promise!../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 */
// ...
    loaders: [
      {
        test: /\.ts$/,
        loaders: [
          "awesome-typescript-loader",
          "angular2-load-children-loader" // this loader replace loadChildren value to function to call require.
        ],
      }
    ],

// ...

  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.

ng2-lazy-load-demo's People

Contributors

quramy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.