cannot find module in angular Lazy Loading

我与影子孤独终老i 提交于 2021-01-02 05:50:52

问题


I have an angular project successfully on the Mac environment

Angular CLI: 7.0.5
Node: 8.11.3
OS: darwin x64
Angular: 7.0.3

Now I am running the same code on ubuntu 18.04 with the setup

Angular CLI: 7.3.9
Node: 12.9.1
OS: linux x64
Angular: 7.2.15

however it is coming with a very weird issue when trying to lazy load another module, I keep getting this error Error: Cannot find module "app/website/site.module"

Here is my project structure

and app-routing.module.ts

 const routes: Routes = [    
      {
        path: 'site',
        loadChildren: 'app/website/site.module#SiteModule',
      }
    ]

the routing is used to work in mac but failed in ubuntu

I looked up a different solution

const rootRoutes: Routes = [
  { path: 'site', loadChildren: './website/site.module#SiteModule' }
];

but still it failed to work.


回答1:


I created an example project on stackblitz

Explanation:

So first of all, you have to create your routes like this:

const routes: Routes = [
  // this will get lazy loaded
  { 
    path: 'lazyload',
    loadChildren: () => import('./module/module.module').then(mod => mod.ModuleModule) 
  },

  // default route
  { 
    path: '**',
    component: Component1Component
  }
];

Then add it to app module (root module):

@NgModule({
  imports:      [
    // your imports here ...
    // add your routes (forRoot() is only in the root module!)
    RouterModule.forRoot(routes)
  ],
  // your providers, declarations, etc.
})
export class AppModule { }

Then you have to add routes to the child module (in this case ModuleModule):

const routes: Routes = [
  { path: 'route1', component: Component2Component },
  { path: '', component: Component3Component }
];

@NgModule({
  imports: [
    // your imports here ... 
    RouterModule.forChild(routes)
  ],
  declarations: [Component2Component, Component3Component]
})
export class ModuleModule { }

now it should work, if you have any problems I will be here :)




回答2:


I had a similar problem, in my code it was the same path like your

  {
      path: 'admin', loadChildren: './admin/admin.module#AdminModule'
  }

and after I changed file tsconfig.app.json

it began to work and find child route, on attached image you can see changes that i done




回答3:


In my particular case, Pavel B. solution worked best. No warnings at compilation or runtime!

  {
    path: 'users',
    loadChildren: () => import('./users/users.module').then(mod => mod.UsersModule)
  }

Using:

Angular CLI: 9.1.7 Node: 12.14.0 OS: darwin x64

Angular: 9.1.9 ... animations, common, compiler, compiler-cli, core, forms ... platform-browser, platform-browser-dynamic, router Ivy Workspace: Yes

Package and Versions

@angular-devkit/architect         0.901.7
@angular-devkit/build-angular     0.901.7
@angular-devkit/build-optimizer   0.901.7
@angular-devkit/build-webpack     0.901.7
@angular-devkit/core              9.1.7
@angular-devkit/schematics        9.1.7
@angular/cli                      9.1.7
@ngtools/webpack                  9.1.7
@schematics/angular               9.1.7
@schematics/update                0.901.7
rxjs                              6.5.5
typescript                        3.8.3
webpack                           4.42.0



回答4:


You can use like this:

const rootRoutes: Routes = [ { path: 'site', loadChildren: () => SiteModule } ];



来源:https://stackoverflow.com/questions/57764623/cannot-find-module-in-angular-lazy-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!