如何在其他人中使用组件?

How to use component in others?

我有一个组件 TreeComponent。它没有模块。只是组件anf模板。

如何在其他组件中使用该组件?

当我将组件添加到另一个组件的声明部分时:

@NgModule({
declarations: ["TreeComponent"]
});

我收到一个错误:

Type TreeComponent is part of the declarations of 2 modules

您收到此错误是因为您已将 TreeComponent 添加到两个 Angular 模块的 declarations 数组中。

与其这样做,不如从模块

中导出 TreeComponent
@NgModule({
  declarations: [TreeComponent],
  exports: [TreeComponent]
})
export class MyCustomModule;

然后将您的模块添加到您要在其中使用此 TreeComponent

的任何其他模块的 imports 数组
@NgModule({
  imports: [MyCustomModule, ...],
  ...
})
export class MyOtherModule;

@NgModule({
  imports: [MyCustomModule, ...],
  ...
})
export class SomeOtherModule;

Here's a Sample Code Example for your ref.

截至目前,我刚刚将 CustomComponentsModule 添加到 AppModuleimports 数组中。但是您也可以将 CustomComponentsModule 添加到任何要在 TreeComponent 中使用的 Angular 模块的 imports 数组。