Angular 角度,在其他两个组件中使用一个组件

Angular 角度,在其他两个组件中使用一个组件,angular,Angular,我想在另外两个组件中使用一个组件,如下所示: <jhi-articles-list-component [active]="true"></jhi-articles-list-component> 然后我将该模块导入到我想要使用该组件的模块中,这会导致错误: 无法从ArticlesListModule导出指令ArticlesListComponent 因为它既没有申报也没有进口 您应该添加声明: @NgModule({ declarations: [Articl

我想在另外两个组件中使用一个组件,如下所示:

<jhi-articles-list-component [active]="true"></jhi-articles-list-component>
然后我将该模块导入到我想要使用该组件的模块中,这会导致错误:

无法从ArticlesListModule导出指令ArticlesListComponent 因为它既没有申报也没有进口


您应该添加
声明

@NgModule({
    declarations: [ArticlesListComponent],
    exports: [ArticlesListComponent]
})
export class ArticlesListModule {}
您的组件(在多个位置使用)应该声明并从其模块导出

这将使其在其他模块中可用,并且可以在导入组件的模块(在本例中:
ArticlesListModule
)后使用:

    @NgModule({
       declarations: [ArticlesListComponent],
       exports: [ArticlesListComponent]
    })
    export class ArticlesListModule {}

我向该共享模块添加了一些导入,如CommonModule,它现在可以工作了

@NgModule({
   imports: [ArticleSharingAppSharedLibsModule, ArticleSharingAppSharedCommonModule, 
   ArticleSharingAppAppRoutingModule],
   declarations: [ArticlesListComponent],
   exports: [ArticlesListComponent],
   schemas: [CUSTOM_ELEMENTS_SCHEMA],

})
export class ArticlesListModule {}

我现在遇到了多个模板解析错误,当我在其中一个模块中添加该组件时效果非常好,比如:声明:[HomeComponent ArticlesListComponent],我的意思是,将
ArticlesListModule
导入必须使用它的模块中,然后可以在那里使用
ArticlesListComponent
。你犯了什么错误?我正按照你说的做,我犯了那个错误:@MaciejOstaszewski在你的
articleslitmodule
尝试:
从'@angular/common'导入{CommonModule}
然后将
CommonModule
添加到
@NgModule
中的另一个数组中:
导入:[CommonModule]
@MaciejOstaszewski您是否导入了
ArticlesListModule
,并将其添加到您使用组件的模块的
@NgModule
数组中?是的,我是按照您所说的那样做的错误在于您在HTML中使用ngSwitch的方式。ngSwitch不能在
元素上,只有在像
这样的真实元素上,只有ngSwitchCase才能应用于
元素。看看这个:
@NgModule({
   imports: [ArticleSharingAppSharedLibsModule, ArticleSharingAppSharedCommonModule, 
   ArticleSharingAppAppRoutingModule],
   declarations: [ArticlesListComponent],
   exports: [ArticlesListComponent],
   schemas: [CUSTOM_ELEMENTS_SCHEMA],

})
export class ArticlesListModule {}