[英]Angular How to use Component in another Component
我有 2 个组件,第一个是 modules/pages/home/home.component,第二个是 modules/pages/home/components/banner 我想在 home 组件中使用横幅组件。 当我尝试使用时出现此错误
从您的文件夹结构看来,这两个组件应该是同一个模块的一部分。
所以去home.module.ts
或任何你有的@NgModule
,并确保app-banner
是declarations
数组的一部分。
或者,如果app-banner
是单独模块的一部分,请确保将其他模块添加到home.module
的import
数组中。
您的项目中应该有一个app.module.ts
文件,在其中您会找到@NgModule
,然后应该有declarations
属性(它是一个数组)。 尝试在其中插入BannerComponent
并使用home.component.html
中的选择器调用横幅组件。
在同一个模块
@NgModule({
imports: [...],
declarations: [AComponent,BComponent,...],
})
export class AllInOneModule {}
在不同的模块中
//Module "A"
@NgModule({
imports: [...],
declarations: [AComponent,...],
exports: [AComponent],
})
export class AModule {}
//Module "B"
@NgModule({
imports: [AModule...],
declarations: [BComponent,...],
})
export class BModule {}
导入并在 app.moudle.ts 中声明组件后,它可以工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.