简体   繁体   English

如何在另一个内部使用组件

[英]How to use a component inside another

I cannot use app.module as this is a large application. 我无法使用app.module因为这是一个大型应用程序。 The component I want to use this component in DOES NOT HAVE a module file. 我要在该组件中使用此组件的组件没有模块文件。

I have tried importing it and using @Ngmodule but doesn't work. 我尝试导入它并使用@Ngmodule但是不起作用。

Uncaught Error: Template parse errors: 'x' is not a known element: 未捕获的错误:模板解析错误:'x'不是已知元素:

If your application is too big it's better to make different modules for features. 如果您的应用程序太大,则最好为功能创建不同的模块。 As an example you can make an auth module and add your login and register components there. 例如,您可以制作一个auth module然后在其中添加您的登录名和注册组件。 You have to declare your components which are belong to each module, within that child module. 您必须声明属于该子模块内每个模块的组件。 For that module, you can add a separate routing file as well. 对于该模块,您也可以添加一个单独的路由文件。 If you want to use one of your component's selector as , you need to export it in your child module. 如果要将组件的选择器之一用作,则需要将其导出到子模块中。 Don't forget to import CommonModule in your child module. 不要忘记在您的子模块中导入CommonModule

@NgModule({
declarations: [
 BreadcrumbsComponent,
 ModalLoginComponent,
],
imports: [
 CommonModule,
 RouterModule,
 FormsModule,
 NgxSpinnerModule
],
exports: [
 BreadcrumbsComponent,
 ModalLoginComponent
]
})
export class SharedModule {}

And in your app.module you have to import your child module. 并且在您的app.module您必须导入您的子模块。

@NgModule({
declarations: [
 AppComponent,
 WorldWideComponent,
 HeaderComponent,
 FooterComponent,
 PageNotFoundComponent
],
imports: [
 BrowserModule,
 BrowserAnimationsModule,
 AppRoutesModule,
 SharedModule,
 CoreModule,
 UserModule,
 NgbModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {
}

You don't put a component in directives 您不会在指令中放置组件

You register it in @NgModule declarations: 您在@NgModule声明中注册它:

@NgModule({
imports: [ BrowserModule ],
declarations: [ App , MyChildComponent ],
bootstrap: [ App ]
})

and then You just put it in the Parent's Template HTML as : <my-child></my-child> 然后您只需将其放在“父母的模板” HTML中,如下所示: <my-child></my-child>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM