1

Webpack 无法找到我尝试使用angular-router-loader延迟加载的模块。

我正在使用以下内容:
Webpack 2.2.1
Angular 5.2
angular-router-loader 0.8.2

通过 npm安装了angular-router-loader,并将加载器添加到我的webpack 配置::

       {
            test: /\.ts$/,
            use: ['awesome-typescript-loader', 'angular2-template-loader'],
            exclude: [/\.(spec|e2e)\.ts$/]
        },
        {
            test: /\.(ts|js)$/,
            loaders: [
              'angular-router-loader'
            ]
        },

项目布局如下所示:

app/
  - app.routing.ts
  authenticated/
    property/
      - property.module.ts
      setup/
         - propertysetup.component.ts

app.routing.ts 中

const appRoutes: Routes = [
  {path: 'property', canActivate: [AuthGuard],
    loadChildren: './authenticated/property/property.module#PropertyModule'
  }
];

属性.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Routes, RouterModule } from '@angular/router';
import { PropertySetupComponent } from './setup/propertysetup.component';

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

@NgModule({
  imports: [
    RouterModule.forChild(routes),
    CommonModule
  ],
  exports: [
    RouterModule
  ],
  declarations: [
    PropertySetupComponent
  ]
})
export class PropertyModule { }

当我启动 webpack 开发服务器时,我可以在控制台中看到angular-router-loader正在替换app.routing.ts的loadChildren部分中定义的字符串:

[angular-router-loader]: --DEBUG--
[angular-router-loader]: File: e:\eclipse_workspace\projectNG\projectFrontend\src\app\app.routing.ts
[angular-router-loader]: Original: loadChildren: './authenticated/property/property.module#PropertyModule'
[angular-router-loader]: Replacement: loadChildren: function() { return new Promise(function (resolve, reject) {  (require as any).ensure([], function (require:
any) {    resolve(require('.\\authenticated\\property\\property.module')

但是我从 webpack 收到以下警告:

WARNING in ./src/app/app.routing.ts
44:84-91 Critical dependency: require function is used in a way in which dependencies cannot be statically extracted

然后,当我尝试在浏览器中访问路径(http://localhost:3000/#/property/setup)时,我从浏览器控制台收到以下堆栈跟踪错误:

 ERROR Error: Uncaught (in promise): Error: Cannot find module "."
    Error: Cannot find module "."
        at webpackMissingModule (eval at 903 (app.js:1618), <anonymous>:44:129)
        at eval (eval at 903 (app.js:1618), <anonymous>:44:208)
        at new ZoneAwarePromise (eval at <anonymous> (polyfills.js:4031), <anonymous>:875:29)
        at loadChildren (eval at 903 (app.js:1618), <anonymous>:44:44)
        at RouterConfigLoader.loadModuleFactory (eval at <anonymous> (vendor.js:156), <anonymous>:4647:109)

我也尝试用绝对路径替换loadChildren中的模块路径。例如

loadChildren: 'app/authenticated/property/property.module#PropertyModule'

但是我从 webpack 得到了完全相同的警告,并且在浏览器控制台中出现了与上面一样的错误。
我想我已经从这里正确地遵循了安装/使用说明:https ://github.com/brandonroberts/angular-router-loader

非常感谢任何建议/帮助。

4

0 回答 0