2

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'
        ]
    }

project structure

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

3
  • You need something like this: npmjs.com/package/angular-router-loader Commented Oct 3, 2017 at 5:02
  • did you happen to find the answer for this ? Commented Jan 22, 2018 at 14:07
  • hi @brevleq. did you get ans for this? If yes, kindly update here. Commented Apr 5, 2018 at 23:57

3 Answers 3

0

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'},\

        }
    ]
} <-------- ,
Sign up to request clarification or add additional context in comments.

6 Comments

can you show the path to your indexModule from the app folder?
added a print of the path
of course it will not work! I just give you the idea to fix it. well i'll update my answer :)
yes, I've already tried use full path: NavigationError {id: 1, url: "/", error: Error: Cannot find module 'app/theme/pages/default/index/index.module'
Don't use the indexmodule inside the children. Move it to its own path
|
0

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

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

1 Comment

But this is not lazy loading. Is there a solution to still use loadChildren as a string?
-1

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' }
];

1 Comment

the path to IndexModule is relative to AppModule, are you sure the path is correct? Try adding the 'theme' to the path like my example...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.