【发布时间】:2017-08-22 19:12:00
【问题描述】:
我的项目中有以下结构,
src
- component-a
-- component-a.component.ts
-- component-a.html
-- component-a.scss
-- component-a.component.spec.ts
- component-b
-- component-b.component.ts
-- component-b.html
-- component-b.scss
-- component-b.component.spec.ts
- app.component.ts
- app.module.ts
- app.html
我在 app.component.ts 中使用 component-a,因此我已将其包含在 app.module.ts 的声明中。
declarations: [
App, ComponentA,
],
在 app.html 中:<component-a></component-a>
现在我想在 component-a 中添加 component-b。
因此,无论我尝试了什么,当我在 component-a.html 中使用 <component-b></component-b> 时,我都会收到此错误:
Unhandled Promise rejection: Template parse errors:
'component-b' is not a known element:
1. If 'component-b' is an Angular component, then verify that it is part of this module.
2. If 'component-b' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
[ERROR ->]<component-b></component-b>
"): ng:///AppModule/ComponentA.html@7:6 ; Zone: <root> ; Task: Promise.then ; Value:
component-a.html中如何使用component-b?
- 我尝试在component-a.component.ts中导入component-b
- 我尝试在 app.module.ts 中导入和添加组件-b 声明。
【问题讨论】:
-
您必须为组件 A (ng g module 组件-a) 创建单独的模块,将组件-a 和组件-b 移到那里,然后将该模块导入主模块 (app.module)。
-
试试这个fix,这可能会解决你的问题
-
试试这个fix,这可能会解决你的问题
-
您可以使用entryComponents。
-
你为什么不把
ComponentB添加到declarations的app.module就像你对ComponentA所做的那样?
标签: angular angular-cli