使用Angular 4中另一个模块的组件

san*_*dum 13 angular

我在AppModule中有RoomsComponent,它的路由是/ rooms.此外,我有一个懒惰的模块CompaniesModule与组件CompaniesComponent与路由/公司.

当我从AppModule重用RoomsComponent时,我正在尝试构建类似/ companies/{company_id}/rooms /的路由.

我不能这么做很长的RoomsComponent没有在CompaniesModule中声明,但这会引发一个错误,因为组件不能在多个模块中声明.

Deb*_*ahK 42

在共享模块中声明RoomsComponent,然后将该共享模块导入需要它的模块中.以下是我的一个共享模块的示例:

import { NgModule }  from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

import { StarComponent } from './star.component';

@NgModule({
  imports: [ CommonModule],
  exports : [
    CommonModule,
    FormsModule,
    StarComponent
  ],
  declarations: [ StarComponent ],
})
export class SharedModule { }
Run Code Online (Sandbox Code Playgroud)

请注意,我的StarComponent在此声明并导出.然后,它可以在导入此共享模块的模块中声明的任何组件中使用.