【发布时间】:2021-07-21 16:12:12
【问题描述】:
我想拆分我的 Angular 项目以在主模块中加载网站部分,在延迟加载模块中加载应用程序部分。 虽然之前一切正常,但现在我收到很多关于未知组件和指令的错误:
-
来自同一应用程序的组件
<app-downloads-sidebar></app-downloads-sidebar>:错误:projects/nine-gold-tools/src/app/layouts/app-layout/app-layout.component.html:42:13 - 错误 NG8001:“app-downloads-sidebar”不是已知元素:
-
指令无法识别: 错误:projects/nine-gold-tools/src/app/layouts/app-layout/app-layout.component.html:58:53 - 错误
NG8002:无法绑定到“dropup”,因为它不是“div”的已知属性。
58
库元素(来自 NineGoldLibModule)
错误:projects/nine-gold-tools/src/app/application/ramlConverter/raml-converter-page/raml-converter-page.component.html:41:21 - 错误 NG8001: 'lib-file-input ' 不是已知元素:
最后是子页面的路由器出口
错误:projects/nine-gold-tools/src/app/layouts/app-layout/app-layout.component.html:93:9 - 错误 NG8001:'router-outlet' 不是已知元素:
在应用程序路由中我从:
{ path: 'app', component: AppLayoutComponent, canActivate: [NewAuthGuard], children: [ { path: 'downloads', component: DownloadsPageComponent, canActivate: [NewAuthGuard] }, { path: 'raml-converter', component: RamlConverterPageComponent, canActivate: [NewAuthGuard] }, { path: 'json-converter', component: JsonConverterPageComponent, canActivate: [NewAuthGuard] }, { path: '', redirectTo: 'raml-converter', pathMatch: 'full' } ]},到:
{ path: 'app', loadChildren: './application/tools-app.module.ts' },
在 tools-app.module.ts 中我声明了所有组件(从 app 模块中删除了声明)并且只做这些导入:
declarations: [ DownloadsPageComponent, DownloadsSidebarComponent, AppLayoutComponent, RamlConverterPageComponent, RamlConverterSidebarComponent, JsonConverterSidebarComponent, JsonConverterPageComponent, ], imports: [ CommonModule, NineGoldLibModule, ToolsAppRoutingModule ]NineGoldLibModule 也是在 app-module.ts 中导入的工作区库
最后是我的 tools-app-routing.module.ts:
const routes: Routes = [ { path: '', component: AppLayoutComponent, canActivate: [NewAuthGuard], children: [ { path: 'downloads', component: DownloadsPageComponent, canActivate: [NewAuthGuard] }, { path: 'raml-converter', component: RamlConverterPageComponent, canActivate: [NewAuthGuard] }, { path: 'json-converter', component: JsonConverterPageComponent, canActivate: [NewAuthGuard] }, { path: '', redirectTo: 'raml-converter', pathMatch: 'full' } ]} ]; @NgModule({ imports: [RouterModule.forChild(routes)], exports: [RouterModule] }) export class ToolsAppRoutingModule { }我在网上找不到任何有效的解决方案。
【问题讨论】:
-
{ path: 'app', loadChildren: './application/tools-app.module.ts' }延迟加载语法不正确 -
@GaurangDhorda,应该是什么?
-
{ path: 'app', loadChildren: () => import('./application/tools-app.module').then(m => m.ToolsAppModule) } -
这个错误出现在哪一行?并且在 loadChildren 的 import('') 中仔细检查路径是否正确
-
是的。谢谢。您的 cmets 将我引导至解决方案。我认为模板中的第一个错误就像多米诺骨牌效应。对我来说仍然很奇怪,为什么我需要再次在lazyLoaded模块中加载那个外部库。
标签: angular angular-router angular-library angular-lazyloading