[英]Angular How to use Component in another Component
I have 2 components, first one is modules/pages/home/home.component and the second one is modules/pages/home/components/banner I want to use banner component in home component.我有 2 个组件,第一个是 modules/pages/home/home.component,第二个是 modules/pages/home/components/banner 我想在 home 组件中使用横幅组件。 when i try to use i get this error
当我尝试使用时出现此错误
From your folder structure it looks like both components should be part of the same module.从您的文件夹结构看来,这两个组件应该是同一个模块的一部分。
So go to home.module.ts
or whatever @NgModule
you have, and make sure app-banner
is part of the declarations
array.所以去
home.module.ts
或任何你有的@NgModule
,并确保app-banner
是declarations
数组的一部分。
Alternatively, if app-banner
is part of separate module, be sure to add that other module inside home.module
's import
array.或者,如果
app-banner
是单独模块的一部分,请确保将其他模块添加到home.module
的import
数组中。
There should be an app.module.ts
file in your project, inside it you'll find @NgModule
, and then there should be declarations
property (it's an array).您的项目中应该有一个
app.module.ts
文件,在其中您会找到@NgModule
,然后应该有declarations
属性(它是一个数组)。 Try to insert your BannerComponent
in there and call your banner component using it's selector in home.component.html
.尝试在其中插入
BannerComponent
并使用home.component.html
中的选择器调用横幅组件。
In the same module在同一个模块
@NgModule({
imports: [...],
declarations: [AComponent,BComponent,...],
})
export class AllInOneModule {}
In different Module在不同的模块中
//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.