0

I every body, I try to use lazy loading in my angular 2 app generated by angular cli: the router works but no additional chunk file are generated.

this is my AppModule:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';

import { APP_ROUTES } from "./app.routing";
import { RouterModule } from '@angular/router';

//ng2 bootstrap
import { Ng2BootstrapModule } from 'ng2-bootstrap/ng2-bootstrap';
import { VotesModule } from './votes/votes.module';
import { InsertComponent } from './insert/insert.component';
import { VotesComponent } from './votes/votes.component';

@NgModule({
  declarations: [
    AppComponent,
    VotesComponent,
    InsertComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    Ng2BootstrapModule,
    RouterModule.forRoot(APP_ROUTES),
    VotesModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

my app.routing:

import { Routes } from "@angular/router";

import { InsertComponent } from "./insert/insert.component";
import { VotesComponent } from "./votes/votes.component";

export const APP_ROUTES: Routes = [
    { path: '', redirectTo: '/vote', pathMatch: 'full' },
    { path: 'vote', component: VotesComponent },
    { path: 'auth', component: InsertComponent, loadChildren: 'app/insert/insert.module#InsertModule'}
];

the InsertModule:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from "@angular/router";

import { INSERTVOTES_ROUTES } from './insert-vote.routing';

import { InsertListComponent } from './insert-list/insert-list.component';
import { InsertVotersComponent } from './insert-voters/insert-voters.component';

@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild(INSERTVOTES_ROUTES)
  ],
  declarations: [ InsertListComponent, InsertVotersComponent]
})
export class InsertModule { }

and the insert-vote.routing:

import { Routes } from "@angular/router";

import { InsertVotersComponent } from "./insert-voters/insert-voters.component";
import { InsertListComponent } from "./insert-list/insert-list.component";

export const INSERTVOTES_ROUTES: Routes = [
    { path: '', redirectTo: 'insert', pathMatch: 'full' },
    { path: 'insert', component: InsertVotersComponent },
    { path: 'voters', component: InsertListComponent }
];

this is the screenshot in wich there isn't chunk file

Is there some other dependecies I should install? Or any Idea? Here there is my package.json:

{
  "name": "votes",
  "version": "0.0.0",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
    "start": "node ./bin/www",
    "lint": "tslint \"src/**/*.ts\"",
    "test": "ng test",
    "pree2e": "webdriver-manager update",
    "e2e": "protractor"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "2.2.3",
    "@angular/compiler": "2.2.3",
    "@angular/core": "2.2.3",
    "@angular/forms": "2.2.3",
    "@angular/http": "2.2.3",
    "@angular/platform-browser": "2.2.3",
    "@angular/platform-browser-dynamic": "2.2.3",
    "@angular/router": "3.2.3",
    "bcryptjs": "^2.3.0",
    "body-parser": "^1.15.2",
    "bootstrap": "^3.3.7",
    "cookie-parser": "^1.4.3",
    "core-js": "^2.4.1",
    "express": "^4.14.0",
    "jsonwebtoken": "^7.2.1",
    "mongoose": "^4.7.2",
    "mongoose-unique-validator": "^1.0.3",
    "morgan": "^1.7.0",
    "ng2-bootstrap": "^1.1.16",
    "rxjs": "5.0.0-beta.12",
    "serve-favicon": "^2.3.2",
    "ts-helpers": "^1.1.1",
    "zone.js": "^0.6.23"
  },
  "devDependencies": {
    "@angular/compiler-cli": "2.2.3",
    "@types/jasmine": "2.5.38",
    "@types/node": "^6.0.42",
    "angular-cli": "1.0.0-beta.22-1",
    "codelyzer": "~2.0.0-beta.1",
    "jasmine-core": "2.5.2",
    "jasmine-spec-reporter": "2.5.0",
    "karma": "1.2.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-remap-istanbul": "^0.2.1",
    "protractor": "4.0.9",
    "ts-node": "1.2.1",
    "tslint": "^4.0.2",
    "typescript": "~2.0.3",
    "webdriver-manager": "10.2.5"
  }
}

3 Answers 3

1

Your AppModule should have a route that loads your InsertModule via loadChildren. That's how the CLI can generate a separate bundle.

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

Comments

1

You need to update APP_ROUTES by removing component from lazy loaded route

 { path: 'auth', loadChildren:'app/insert/insert.module#InsertModule'}

And update your insert-vote.routing

{ path: '', component: InsertComponent },
{ path: 'insert', component: InsertVotersComponent },
{ path: 'voters', component: InsertListComponent }

Comments

0

Remove " InsertComponent" from declarations in the AppModule And remove "component: InsertComponent" from "APP_ROUTES" , I suggest you take a look at Lazy-Loading route configuration

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.