【发布时间】:2019-07-28 19:05:33
【问题描述】:
我正在编写一个 Angular 8 应用程序,我正在尝试延迟加载一些模块。但是,当页面加载时,我收到此错误:
错误:组件 ProfileComponent 不是任何 NgModule 或 模块尚未导入到您的模块中。
具体来说,我希望在命中路由并加载组件时加载模块。问题是组件是模块的一部分。
这是子模块的代码,其中定义了组件:
@NgModule({
imports: [],
declarations: [
...
ProfileComponent
],
exports: [
...
ProfileComponent
]
})
export class ProfileModule { }
这是试图动态加载子模块的父模块中的路由定义:
const routes: Routes = [
{
path: 'projects/profile/:id', component: ProfileComponent,
loadChildren: () => {
return import('./profile/profile.module').then(m => m.ProfileModule);
},
children: [
{ path: 'list/:id', component: ListComponent, outlet: 'sidebar' },
{ path: 'risk/:id', component: RiskComponent, outlet: 'sidebar' },
...
]
}
有没有办法在导航时加载组件和模块?我尝试将有问题的 ProfileComponent 移动到父模块中,但是当我导航到页面时,它希望它的所有子组件都存在(这与延迟加载相反)。我可以创建一个不使用子组件的登录页面,我可以在其中单击以重定向到动态加载子模块的路由,但我宁愿不向应用程序添加另一层单击。
【问题讨论】:
-
你的父模块不应该直接列出子模块,它应该只使用
loadchildren。然后为您的子模块提供自己的路由文件,以将请求转发到其组件。 ;毕竟,只有当你的父级没有对子模块内容的引用时,延迟加载才是延迟加载。而且你也不需要导出组件。