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.
To lazy load Angular modules, use loadChildren (instead of component) in your AppRoutingModule routes configuration as follows. In the lazy-loaded module's routing module, add a route for the component.
A common error when lazy-loading modules is importing common modules in multiple places within an application. Test for this condition by first generating the module using the Angular CLI and including the --route route-name parameter, where route-name is the name of your module.
You can check to see that a module is indeed being lazy loaded with the Chrome developer tools. In Chrome, open the developer tools by pressing Cmd+Option+i on a Mac or Ctrl+Shift+j on a PC and go to the Network Tab. Click on the Orders or Customers button.
If the Angular CLI generates an error when you use the --route parameter, but runs correctly without it, you might have imported the same module in multiple places. Remember, many common Angular modules should be imported at the base of your application. For more information on Angular Modules, see NgModules.
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 :)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With