在角度延迟加载中找不到模块 [英] cannot find module in angular Lazy Loading
问题描述
我在 Mac 环境下成功创建了一个 angular 项目
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
现在我在 ubuntu 18.04 上使用设置运行相同的代码
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
然而,在尝试延迟加载另一个模块时出现了一个非常奇怪的问题,我不断收到此错误错误:找不到模块app/website/site.module"
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"
这是我的项目结构
和 app-routing.module.ts
and app-routing.module.ts
const routes: Routes = [
{
path: 'site',
loadChildren: 'app/website/site.module#SiteModule',
}
]
路由在mac下可以使用,在ubuntu下失败
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' }
];
但是还是不行.
推荐答案
说明:
所以首先,你必须像这样创建你的路线:
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 { }
然后您必须向子模块(在本例中为 ModuleModule)添加路由:
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 :)
这篇关于在角度延迟加载中找不到模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!