简体   繁体   中英

Angular with Webpack - loadChildren can't find the module

I'm trying load an admin I bought that works with Angular4, however I'm having some trouble configuring the project to run with Webpack:

const routes: Routes = [
{
    "path": "",
    "component": ThemeComponent,
    "canActivate": [AuthGuard],
    "children": [
        {
            "path": "index",
            "loadChildren": './pages/default/index/index.module#IndexModule'
        }
    ],
    {
        "path": "**",
        "redirectTo": "404",
        "pathMatch": "full"
    }
];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule]
})
export class ThemeRoutingModule {}

when I run the app, I get this message in console:

NavigationError {id: 1, url: "/", error: Error: Cannot find module './pages/default/index/index.module'. at eval (eval at ./src/main/weba…} error : Error: Cannot find module './pages/default/index/index.module'. at eval (eval at ./src/main/webapp/admin/app lazy recursive ( http://localhost:8080/admin/app/main.bundle.js:6:1 ), :5:9) at ZoneDelegate.invoke (webpack-internal:///./node_modules/zone.js/dist/zone.js:391:26) at Object.onInvoke (webpack-internal:///./node_modules/@angular/core/@angular/core.es5.js:4094:33) at ZoneDelegate.invoke (webpack-internal:///./node_modules/zone.js/dist/zone.js:390:32) at Zone.run (webpack-internal:///./node_modules/zone.js/dist/zone.js:141:43) at eval (webpack-internal:///./node_modules/zone.js/dist/zone.js:831:57) at ZoneDelegate.invokeTask (webpack-internal:///./node_modules/zone.js/dist/zone.js:424:31) at Object.onInvokeTask (webpack-internal:///./node_modules/@angular/core/@angular/core.es5.js:4085:33) at ZoneDelegate.invokeTask (webpack-internal:///./node_modules/zone.js/dist/zone.js:423:36) at Zone.runTask (webpack-internal:///./node_modules/zone. js/dist/zone.js:191:47) id : 1 url : "/"

I know the path to the module is correct, as I can import this module without problem in the same file:

import {IndexModule} from './pages/default/index/index.module';

My Webpack config is using this to proccess the typescript:

module: {
    rules: [{
        test: /\.ts$/,
        enforce: 'pre',
        loaders: 'tslint-loader',
        exclude: ['node_modules', new RegExp('reflect-metadata\\' + path.sep + 'Reflect\\.ts')]
    },
    {
        test: /\.ts$/,
        loaders: [
            'awesome-typescript-loader',
            'angular-router-loader?debug=true',
            'angular2-template-loader'
        ]
    }

项目结构

Is there something wrong I'm doing I didn't notice?

The location of your lazy-loaded modules should start at the app folder. Try to add the app keyword to your string like this

loadChildren: 'app/theme/pages/default/index/index.module#IndexModule'},

Also I think you are missing a curly brace here

{
    "path": "",
    "component": ThemeComponent,
    "canActivate": [AuthGuard],
    "children": [
        {
            "path": "index",
             loadChildren: 'app/theme/pages/default/index/index.module#IndexModule'},\

        }
    ]
} <-------- ,

Instead of import using module path, I discovered I can import using function. This way it worked:

{
    "path": "index",
    "loadChildren": () => IndexModule
}

If you're using webpack, you need the angular-router-loader :

loaders: [
  {
    test: /\.ts$/,
    loaders: [
      'awesome-typescript-loader',
      'angular-router-loader'
    ]
  }
]

Create a module which loads additional routes:

const routeModule:ModuleWithProviders = RouterModule.forChild(routes);
@NgModule({
    imports:      [ routeModule ],
    declarations: [ ... ]
})
export class InboxModule{ }

Create your routes and reference the lazy-loaded module (the path is relative):

import { Routes } from '@angular/router';

export const routes: Routes = [
  { path: 'index', loadChildren: './theme/pages/default/index/index.module#IndexModule' }
];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM