Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何在其他系统中使用组件?_Angular_Angular7_Angular8 - Fatal编程技术网

Angular 如何在其他系统中使用组件?

Angular 如何在其他系统中使用组件?,angular,angular7,angular8,Angular,Angular7,Angular8,我有一个组件TreeComponent。它没有模块。只是组件和模板 如何在其他组件中使用此组件 当我将组件添加到另一个组件的声明部分时: @NgModule({ declarations: ["TreeComponent"] }); 我得到一个错误: 类型TreeComponent是两个模块声明的一部分 之所以出现此错误,是因为已将TreeComponent添加到两个角度模块的声明数组中 只需从模块导出TreeComponent,而不必这样做 @NgModule({ declaration

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

如何在其他组件中使用此组件

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

@NgModule({
declarations: ["TreeComponent"]
});
我得到一个错误:

类型TreeComponent是两个模块声明的一部分


之所以出现此错误,是因为已将
TreeComponent
添加到两个角度模块的
声明
数组中

只需从模块导出
TreeComponent
,而不必这样做

@NgModule({
  declarations: [TreeComponent],
  exports: [TreeComponent]
})
export class MyCustomModule;
然后将您的模块添加到要在其中使用此
TreeComponent的任何其他模块的
imports
数组中

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


这是给你的裁判的一封信


现在,我刚刚将
CustomComponentsModule
添加到
AppModule
imports
数组中。但是,您也可以将
CustomComponentsModule
添加到要在中使用
treecocomponent
的任何角度模块的
imports
数组中。

Sholud我是为该组件创建模块还是在模块顶部注册该组件?仅在app.Module.ts文件中注册该组件。删除任何其他声明。但我只需要两个组件中的此组件,我不想在vainI中加载此组件无法导出,因为
声明:[TreeComponent],
表示错误,两个声明
MyCustomModule
是treeModule还是其他模块?因此,您建议将其包装在模块中?我不确定它的名称是否正确?我的意思是,该模块与树组件相关,或者它是外部模块?
@NgModule({
  imports: [MyCustomModule, ...],
  ...
})
export class SomeOtherModule;