angular - Angular2 RC7 和 angular-cli webpack 中的延迟加载

标签 angular typescript webpack angular2-routing angular-cli

我已经使用 ng new 创建了一个新应用程序,但是当我尝试配置一个模块以使用延迟加载进行加载时,我不断收到无法找到该模块的错误消息。

EXCEPTION: Uncaught (in promise): Error: Cannot find module 'app/home/home.module'

package.json

"dependencies": {
    "@angular/common": "2.0.0-rc.7",
    "@angular/compiler": "2.0.0-rc.7",
    "@angular/core": "2.0.0-rc.7",
    "@angular/forms": "2.0.0-rc.7",
    "@angular/http": "2.0.0-rc.7",
    "@angular/platform-browser": "2.0.0-rc.7",
    "@angular/platform-browser-dynamic": "2.0.0-rc.7",
    "@angular/router": "3.0.0-rc.3",
    "core-js": "^2.4.1",
    "rxjs": "5.0.0-beta.12",
    "ts-helpers": "^1.1.1",
    "zone.js": "^0.6.21"
  },
  "devDependencies": {
    "@types/jasmine": "^2.2.30",
    "angular-cli": "1.0.0-beta.11-webpack.9-4",
    "codelyzer": "~0.0.26",
    "jasmine-core": "2.4.1",
    "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.5",
    "ts-node": "1.2.1",
    "tslint": "3.13.0",
    "typescript": "2.0.2"
  }
  • angular2 RC7
  • angular-cli: 1.0.0-beta.11-webpack.9-4
  • 节点:6.5.0
  • 操作系统:win32 x64

app.routing.ts:

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

const appRoutes: Routes = [{
    path: '',
    redirectTo: 'home',
    pathMatch: 'full'
},
{
    path: `home`,
    loadChildren: `app/home/home.module`
}];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

应用程序模块.ts

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 { routing } from './app.routing';

@NgModule({
  declarations: [
     AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    routing
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

应用程序组件.ts

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

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.css']
})
export class AppComponent {
  title = 'app works!';
}

app.component.html

<router-outlet></router-outlet>

我确定我遗漏了一些愚蠢的东西,但我尝试了各种选择,但到目前为止没有任何效果。根据我的配置,我是否遗漏了什么?

提前致谢, 斯蒂芬

最佳答案

使用 RC7,您可以执行以下操作:

const appRoutes: Routes = [
  {
    path: 'home',
    loadChildren: 'app/home/home.module#HomeModule',
  }
];

不要忘记“#

然后杀掉 ng serve 并重启

它将与 angular-cli@webpack 一起工作

P.S: Angular 约定是在延迟加载的文件夹前加上 (+),例如+首页/

关于angular - Angular2 RC7 和 angular-cli webpack 中的延迟加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39493782/

相关文章:

typescript - 调度 thunk Action 创建者在 TypeScript 中不匹配

javascript - 使用 webpack 将 font-awesome 添加到项目中

html - 页面在 IE11 上的显示方式问题

angular - 在组件中显示服务变量

selenium-webdriver - 如何在 typescript 测试中增加每个套件的 Mocha 超时

javascript - 未捕获的语法错误 : Cannot use import statement outside a module in node js

javascript - 无法使用 Webpack DevServer 接收我的 js 和 css 文件

javascript - 如何使用 Angular2 传递有关如何在管道中过滤的元数据?

angular - 如何将子组件传递给父兄弟组件?

javascript - Typescript 数组映射 vs 过滤器 vs?