【问题标题】:Angular-cli webpack lazy loading not workingAngular-cli webpack延迟加载不起作用
【发布时间】:2016-09-13 08:22:02
【问题描述】:

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

代码如下:

app/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);

app/+dashboard/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);

app/+login/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);

app/+dashboard/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 { }

app/+login/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 { }

app/+dashboard/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() {
  }

}

app/+login/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 bundle 异步加载”。但是没有来自仪表板组件的内容(构造函数和 ngOnInit 没有被调用)。

在“登录”路径上,我有:“Login 异步加载包”。但是登录组件没有内容(构造函数和ngOnInit没有被调用)。

【问题讨论】:

标签: angular webpack lazy-loading angular-cli


【解决方案1】:

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

{path: 'lazy-module', loadChildren: 'app/lazy-module/lazy.module#LazyModule'}{path: 'lazy-module', loadChildren: './lazy-module/lazy.module#LazyModule'} 假设相对于路由器配置文件的相对路径。

我们现在需要注意一个问题:

每次编辑惰性模块路由配置时,都需要手动重启 webpack,方法是停止当前的npm start,然后重新运行npm start

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

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

使用 Angular 编码愉快!

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 你是说所有的桶?
    • @ImNotAnUser 我删除了子模块目录和共享文件夹中的桶。在那之后它起作用了......
    • 您如何判断它是否有效?我可以看到我的模块是延迟加载的(console.log 在构造函数中)但是我无法确定 Angular CLI 是否只加载它需要的 JS 块(当我在 prod 目标模式下构建时只有 1 个包)
    猜你喜欢
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 2018-10-21
    相关资源
    最近更新 更多