Angular-cli webpack延迟加载不起作用

时间:2016-09-13 08:22:03

标签: angular webpack lazy-loading angular-cli

我正在尝试使用angular2 RC6与最新的angular-cli(主分支)进行异步路由。 但是我被卡住了......

以下是代码:

应用程序/ app.routing.ts

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

import { AuthGuardService } from './shared';

const routes: Routes = [
  {
    path: '',
    loadChildren: () => require('es6-promise!./+dashboard/dashboard.module')('DashboardModule'),
    canActivate: [AuthGuardService],
    pathMatch: 'full'
  },
  {
    path: 'login',
    loadChildren: () => require('es6-promise!./+login/login.module')('LoginModule'),
  }
];

export const routing: ModuleWithProviders = RouterModule.forRoot(routes);

应用程序/ +仪表板/ dashboard.routing.ts

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

import { DashboardComponent } from './';

const routes: Routes = [
  {
    path: '',
    component: DashboardComponent
  }
];

export const dashboardRouting: ModuleWithProviders = RouterModule.forChild(routes);

应用程序/ +登录/ login.routing.ts

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

import { LoginComponent } from './';

const routes: Routes = [
  {
    path: '',
    component: LoginComponent
  }
];

export const loginRouting: ModuleWithProviders = RouterModule.forChild(routes);

应用程序/ +仪表板/ dashboard.module.ts

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

import { DashboardComponent, dashboardRouting } from './';

console.log('`Dashboard` bundle loaded asynchronously');

@NgModule({
  imports: [
    CommonModule,
    dashboardRouting
  ],
  exports: [
    DashboardComponent
  ],
  declarations: [DashboardComponent]
})
export class DashboardModule { }

应用程序/ +登录/ login.module.ts

import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';

import { LoginComponent, loginRouting } from './';
import { MdModule } from '../shared';

console.log('`Login` bundle loaded asynchronously');

@NgModule({
  imports: [
    CommonModule,
    loginRouting,
    FormsModule,
    ReactiveFormsModule,
    MdModule.forRoot()
  ],
  exports: [
    LoginComponent
  ],
  declarations: [LoginComponent]
})
export class LoginModule { }

应用程序/ +仪表板/ dashboard.component.ts

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

@Component({
  selector: 'my-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.scss']
})
export class DashboardComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

应用程序/ +登录/ login.component.ts

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';

import { UserService } from '../shared';

@Component({
  selector: 'my-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {

  private loginForm: FormGroup;
  private usernameCtrl: FormControl;
  private passwordCtrl: FormControl;

  constructor(private fb: FormBuilder, private userService: UserService, private router: Router) {
    this.usernameCtrl = fb.control('', Validators.required);
    this.passwordCtrl = fb.control('', Validators.required);

    this.loginForm = fb.group({
      username: this.usernameCtrl,
      password: this.passwordCtrl
    });
  }

  ngOnInit() {
    if (this.userService.isAuthenticated()) {
      this.router.navigate(['/']);
    }
  }

  authenticate() {
    this.userService.authenticate(this.usernameCtrl.value, this.passwordCtrl.value)
      .then(() => this.router.navigate(['/']));
  }

}

编译和运行时都没有错误。但是没有加载异步组件。

在''路径上,在控制台中我有:“Dashboard捆绑异步加载”。但是没有来自仪表板组件的内容(不会调用构造函数和ngOnInit)。

在'登录'路径上,我有:“Login捆绑异步加载”。但是没有来自登录组件的内容(不会调用构造函数和ngOnInit)。

2 个答案:

答案 0 :(得分:4)

使用截至今天的最新angular-cli,beta.21,我们可以使用绝对路径和相对路径来处理延迟加载模块,如下所示:

假设相对路径配置文件的相对路径为

{path: 'lazy-module', loadChildren: 'app/lazy-module/lazy.module#LazyModule'}{path: 'lazy-module', loadChildren: './lazy-module/lazy.module#LazyModule'}

我们现在需要了解一些问题:

每次编辑延迟模块路由配置时,我们都需要通过停止当前npm start并重新运行npm start来手动重启webpack。

Angular-cli在新的启动时对延迟加载模块进行了一些代码操作,但是由于性能问题,它不能在webpack的自动重新编译中执行此操作。

有一天可能不是这样。我很期待。

使用Angular进行快乐编码!

答案 1 :(得分:2)

通过删除桶使用来解决...