![](/img/trans.png)
[英]In Angular 8, how do I pass a component into another component's input and then use it imperatively in a router module
[英]How to use component in a in another module/router
我正在嘗試使用另一個模塊中的另一個組件,但它在我的路由器插座中不起作用。
應用模塊
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 { }
主模塊
import { NgModule } from '@angular/core';
import { routing } from '../app.routing';
import { ProductComponent } from './product/product.component';
@NgModule({
imports: [
routing
],
declarations: [ProductComponent]
})
export class MainModule { }
我的問題是,當我嘗試在product.component.html
調用鍵盤組件時,它會給我一個錯誤,說它不存在。 但是,當我在main.component.html
調用它時,它工作正常。
如果你的組件應該被 app 模塊和主模塊使用,它應該放在主模塊或另一個共享模塊中。 以下示例顯示了如何使用共享模塊。
應用模塊聲明
@NgModule({
declarations: [
AppComponent,
MainComponent,
],
imports: [
CommonModule,
MainModule,
SharedModule,
routing
],
bootstrap: [AppComponent]
})
export class AppModule { }
主模塊聲明
@NgModule({
imports: [
CommonModule,
routing,
SharedModule
],
declarations: [ProductComponent]
})
export class MainModule { }
共享模塊聲明
@NgModule({
imports: [ CommonModule ],
declarations: [ KeypadComponent ],
exports: [ KeypadComponent ]
})
export class SharedModule { }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.