简体   繁体   English

如何在另一个模块/路由器中使用组件

[英]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.

相关问题 在 Angular 8 中,如何将一个组件传递到另一个组件的输入中,然后在路由器模块中强制使用它 - In Angular 8, how do I pass a component into another component's input and then use it imperatively in a router module 如何在entryComponents中使用模块中的组件另一个模块? - How to use component from module in entryComponents another module? 使用另一个模块的组件 - Use component from another module 在另一个模块中使用在 app 模块中声明的组件 - Use component declared in app module in another module 如何从Angular中的另一个模块引用和使用组件? - How to reference and use a component from another module in Angular? 我无法将一个模块中的组件用于另一个模块组件 - I cannot use component from one module into another module component 为什么要从一个模块导出组件并在另一个模块组件中使用 - Why to Export Component from One Module & Use in Another Module Component Angular - 使用来自另一个模块的组件 - Angular - use component from another module Angular 在另一个模块中使用共享模块组件 - Angular use Shared Modules Component in another module 如何在 AppComponent 以外的其他组件中使用从共享模块导入的 Angular 组件 - How can I use angular component imported from a shared module in another component other than AppComponent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM