我在Mac环境下成功完成了一个angular项目
Angular CLI: 7.0.5
Node: 8.11.3
OS: darwin x64
Angular: 7.0.3
现在我使用设置在 ubuntu 18.04 上运行相同的代码
Angular CLI: 7.3.9
Node: 12.9.1
OS: linux x64
Angular: 7.2.15
但是在尝试延迟加载另一个模块时出现了一个非常奇怪的问题,我不断收到此错误错误:找不到模块“app/website/site.module”
这是我的项目结构
和 app-routing.module.ts
const routes: Routes = [
{
path: 'site',
loadChildren: 'app/website/site.module#SiteModule',
}
]
路由在mac上用过,在ubuntu上失败
我查找了不同的解决方案
const rootRoutes: Routes = [
{ path: 'site', loadChildren: './website/site.module#SiteModule' }
];
但还是不行。
最佳答案
我在 stackblitz 上创建了一个示例项目
解释:
所以首先,你必须像这样创建你的路线:
const routes: Routes = [
// this will get lazy loaded
{
path: 'lazyload',
loadChildren: () => import('./module/module.module').then(mod => mod.ModuleModule)
},
// default route
{
path: '**',
component: Component1Component
}
];
然后将其添加到应用模块(根模块):
@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):
const routes: Routes = [
{ path: 'route1', component: Component2Component },
{ path: '', component: Component3Component }
];
@NgModule({
imports: [
// your imports here ...
RouterModule.forChild(routes)
],
declarations: [Component2Component, Component3Component]
})
export class ModuleModule { }
现在它应该可以工作了,如果你有任何问题我会在这里:)
关于angular - 在 Angular 延迟加载中找不到模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57764623/