我是每个人,我尝试在由角度cli生成的角度2应用程序中使用延迟加载:路由器可以工作,但不会生成额外的块文件。
这是我的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 { }
我的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'}
];
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 { }
和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 }
];
我应该安装一些其他的家属吗?还是任何想法? 这里有我的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"
}
}
答案 0 :(得分:1)
您的AppModule应该有一条通过loadChildren
加载您的InsertModule的路线。这就是CLI如何生成单独的捆绑包。
答案 1 :(得分:1)
您需要通过从延迟加载的路径
中删除组件来更新APP_ROUTES { path: 'auth', loadChildren:'app/insert/insert.module#InsertModule'}
并更新您的insert-vote.routing
{ path: '', component: InsertComponent },
{ path: 'insert', component: InsertVotersComponent },
{ path: 'voters', component: InsertListComponent }
答案 2 :(得分:0)
删除" InsertComponent"来自AppModule中的声明 并删除"组件:InsertComponent"来自" APP_ROUTES" ,我建议你看看Lazy-Loading route configuration