1

I followed this tutorial to add lazy loading to my app. Everything works fine on local server but when I publish my app I get error module cannot be found.I am using Angular Compiler Plugin.Here is my package.json "dependencies": { "@angular/animations": "^5.0.0", "@angular/cdk": "^5.0.0", "@angular/common": "^5.0.0", "@angular/compiler": "^5.0.0", "@angular/compiler-cli": "^5.0.0", "@angular/core": "^5.0.0", "@angular/forms": "^5.0.0", "@angular/http": "^5.0.0", "@angular/material": "^5.0.0", "@angular/material-moment-adapter": "^5.0.0", "@angular/platform-browser": "^5.0.0", "@angular/platform-browser-dynamic": "^5.0.0", "@angular/platform-server": "^5.0.0", "@angular/router": "^5.0.0", "@ngtools/webpack": "^1.9.0", "@types/webpack-env": "1.13.0", "angular-router-loader": "^0.8.0", "angular2-infinite-scroll": "^0.3.5", "angular2-template-loader": "0.6.2", "aspnet-prerendering": "^3.0.1", "aspnet-webpack": "^2.0.1", "awesome-typescript-loader": "3.2.1", "bootstrap": "^3.3.7", "bootstrap-select": "^1.12.4", "css": "2.2.1", "css-loader": "0.28.4", "es6-shim": "0.35.3", "event-source-polyfill": "0.0.9", "expose-loader": "0.7.3", "extract-text-webpack-plugin": "2.1.2", "file-loader": "0.11.2", "font-awesome": "^4.7.0", "hammerjs": "^2.0.8", "html-loader": "0.4.5", "isomorphic-fetch": "2.2.1", "jquery": "^3.2.1", "json-loader": "0.5.4", "moment": "^2.19.4", "ng2-auto-complete": "^0.12.0", "ngx-bootstrap": "^1.9.3", "preboot": "4.5.2", "raw-loader": "0.5.1", "reflect-metadata": "0.1.10", "rxjs": "^5.5.2", "select2": "^4.0.4", "style-loader": "0.18.2", "to-string-loader": "1.1.5", "typescript": "2.4.2", "url-loader": "0.5.9", "webpack": "2.5.1", "webpack-hot-middleware": "2.18.2", "webpack-merge": "4.1.0", "zone.js": "0.8.12" }

I also tried to load my components with a difrent approach like this:

path: 'hero', loadChildren: () => {
    return Promise.resolve(require('./components/hero/hero.module')['HeroModule']);
} 

but then I get error Index signature of object type implicitly has an 'any' type.

1 Answer 1

1

I have created 4 modules which all are lazy loaded in Angular

First thing you have to remove your module which you want to lazy load from app.module.ts file , here is the example and i am lazy loading HomePage module

app.module.ts

   import { NgModule} from '@angular/core';


//modules

   import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
   import { BrowserModule } from '@angular/platform-browser';



  //routing modules

    import { AppRoutingModule } from './app-routing.module';



   @NgModule({
     declarations: [ ],

     imports: [
     BrowserModule,
     BrowserAnimationsModule,
     AppRoutingModule, ],

      bootstrap: [AppComponent] 
      })
     export class AppModule {}

The module which you want to lazy load will come in app-routing-module

app-routing-module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';



 const routes: Routes = [
 { path: "", component: LandingComponent },

 {
  path: "homepage",
  loadChildren: "./homepage/homepage.module#HomePageModule"
 },

 ];

 @NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule]
  })
 export class AppRoutingModule { }

load children is property we use when we want to lazy load any module in our app and #HomePageModule is the name of the module which i want to lazy load

To check that module has been lazy loaded , when you build your angular app it will give you chunk like this

chunk {homepage.module} homepage.module.chunk.js, 
homepage.module.chunk.js.map () 6.41 kB  [rendered]
chunk {inline} inline.bundle.js, inline.bundle.js.map (inline) 5.83 kB 
[entry] [rendered]
chunk {polyfills} polyfills.bundle.js, polyfills.bundle.js.map 
(polyfills) 215 kB [initial] [rendered]
chunk {styles} styles.bundle.js, styles.bundle.js.map (styles) 177 kB 
[initial] [rendered]
chunk {vendor} vendor.bundle.js, vendor.bundle.js.map (vendor) 5.31 MB 
[initial] [rendered]

It shows your module has been lazy loaded successfully

I hope this will help you

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.