首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用功能模块中的组件不工作的angular2 rc5

使用功能模块中的组件不工作的angular2 rc5
EN

Stack Overflow用户
提问于 2016-08-22 00:27:46
回答 2查看 612关注 0票数 0

我有一个应用程序模块和一个功能模块。功能模块中声明了一个名为"AudioPlayerComponent“的组件。我想在应用模块中使用它,但它没有显示。没有错误。

我是不是遗漏了什么?

应用程序模块:

代码语言:javascript
复制
@NgModule({
  imports: [
    BrowserModule,
    HomeModule, 
    ReciterModule, // the feature module which has the audio player
    routing
  ],
  declarations: [
    AppComponent,
    NavComponent
  ],
  providers: [
    appRoutingProviders,
    AudioPlayerService
  ],
  bootstrap: [AppComponent]
})

特性模块:

代码语言:javascript
复制
@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    HttpModule,
    reciterRouting
  ],
  declarations: [
    ReciterDetailComponent, 
    WidgetComponent,
    AudioPlayerComponent  // this is the component I want to also use in the app component
  ],
  providers: [
    AppService,
    RecitersService 
  ]
}) 

功能模块中使用音频播放器(显示)的组件

代码语言:javascript
复制
<div class="reciter-detail">
   ...
    <audio-player></audio-player>

</div>

试图使用音频播放器的App组件(不显示):

代码语言:javascript
复制
<nav-main></nav-main> 
 <div class="container">

    <router-outlet></router-outlet>


    <audio-player></audio-player>
</div>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-22 02:16:56

您必须在Feature的导出中添加AudioPlayerComponent

如果要使用任何组件、管道、功能模块指令到另一个模块,则需要将它们导出

代码语言:javascript
复制
  @NgModule({
   imports: [
     CommonModule,
     FormsModule,
     HttpModule,
     reciterRouting
    ],
   declarations: [
     ReciterDetailComponent, 
     WidgetComponent,
     AudioPlayerComponent  // this is the component I want to also use in the app component
   ],
   //add exports
   exports: [ 
      AudioPlayerComponent
   ],
   providers: [
     AppService,
     RecitersService 
   ]
  }) 

了解有关NgModule属性这里的更多信息。

票数 4
EN

Stack Overflow用户

发布于 2016-08-22 03:07:49

接受的答案是正确的。我结束了阅读指南,创建了一个单独的共享模块。

共享模块:

代码语言:javascript
复制
@NgModule({
    imports: [CommonModule],
    declarations: [
        AudioPlayerComponent
    ],
    exports: [ 
        AudioPlayerComponent,
        CommonModule,
        FormsModule
    ]
})
export class SharedModule {
    static forRoot(): ModuleWithProviders {
        return {
            ngModule: SharedModule,
            providers: [AppService,AudioPlayerService]
        };
    }
}

应用程序模块

代码语言:javascript
复制
@NgModule({
  imports: [
    BrowserModule,
    HomeModule, 
    ReciterModule,
    routing, 
    SharedModule.forRoot()
  ],
  declarations: [
    AppComponent,
    NavComponent
  ],
  providers: [
    appRoutingProviders 
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

特征模块

代码语言:javascript
复制
@NgModule({
  imports: [ 
    HttpModule,
    reciterRouting,
    SharedModule
  ],
  declarations: [
    ReciterDetailComponent, 
    WidgetComponent 
  ],
  providers: [ 
    RecitersService 
  ],
  exports: [ReciterDetailComponent]
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39070009

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档