[英]How to use component in a in another module/router
I'm trying to use another component from another module, but its not working in my router-outlet.我正在尝试使用另一个模块中的另一个组件,但它在我的路由器插座中不起作用。
app.module应用模块
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { MainModule } from './main/main.module';
import { AppComponent } from './app/app.component';
import { KeypadComponent } from './keypad/keypad.component;
import { routing } from './app.routing';
@NgModule({
declarations: [
AppComponent,
MainComponent,
KeypadComponent
],
imports: [
BrowserModule,
CommonModule,
MainModule,
routing
],
bootstrap: [AppComponent]
})
export class AppModule { }
main.module主模块
import { NgModule } from '@angular/core';
import { routing } from '../app.routing';
import { ProductComponent } from './product/product.component';
@NgModule({
imports: [
routing
],
declarations: [ProductComponent]
})
export class MainModule { }
My issue is that when I try to call the Keypad Component in product.component.html
, it will give me an error that says it does not exist.我的问题是,当我尝试在
product.component.html
调用键盘组件时,它会给我一个错误,说它不存在。 However, when I call it in my main.component.html
, it works fine.但是,当我在
main.component.html
调用它时,它工作正常。
If your component should be used by app module and main module it should go in the main module or in another shared module.如果你的组件应该被 app 模块和主模块使用,它应该放在主模块或另一个共享模块中。 Following example show how to use a shared module.
以下示例显示了如何使用共享模块。
AppModule declaration应用模块声明
@NgModule({
declarations: [
AppComponent,
MainComponent,
],
imports: [
CommonModule,
MainModule,
SharedModule,
routing
],
bootstrap: [AppComponent]
})
export class AppModule { }
MainModule declaration主模块声明
@NgModule({
imports: [
CommonModule,
routing,
SharedModule
],
declarations: [ProductComponent]
})
export class MainModule { }
SharedModule declaration共享模块声明
@NgModule({
imports: [ CommonModule ],
declarations: [ KeypadComponent ],
exports: [ KeypadComponent ]
})
export class SharedModule { }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.