简体   繁体   English

用于页眉和页脚的Angular 2功能模块

[英]Angular 2 Feature Modules for Headers and Footers

Right now, I have a generic set-up where I am loading several standard components into a common page header, several components into a common page footer, and the changeable page content is displayed through a <router-outlet> --kind of like so. 现在,我有一个通用设置,其中将几个标准组件加载到一个公共页面页眉中,将几个组件加载到一个公共页面页脚中,并且可变的页面内容通过<router-outlet>显示<router-outlet>有点像所以。

app.component.ts app.component.ts

<div> Several header components loaded and displayed here. </div>
<router-outlet>
<div> Several footer components loaded and displayed here. </div>

All of that works fine--except that I really want to further compartmentalize the site. 所有这些都可以正常工作-除了我真的想进一步划分站点。 There are a total of about 20 components that are loading in the header and the footer. 页眉和页脚中总共加载了大约20个组件。 I really don't want to clutter up my app.module.ts , my app.component.ts , or the main app-routing.module.ts with all of these common components and the routes from their many links. 我真的不想让我的app.module.tsapp.component.ts或主app-routing.module.ts包含所有这些常见组件以及来自其许多链接的路由。 I would much prefer to move them into their own feature modules. 我非常希望将它们移入自己的功能模块。

I have already migrated all of the components for the common page header to a folder called common-header and all of the components for the common page footer to a folder called common-footer . 我已经迁移所有组件的共同页头一个文件夹,名为common-header和所有常见的页脚到一个名为文件夹中部件的common-footer (At least the app directory is not so cluttered.) (至少应用程序目录不是那么混乱。)

I even created modules called common-header.module and common-footer.module , respectively. 我什至分别创建了名为common-header.modulecommon-footer.module模块。 I created a common-header.component and common-footer.component to wrap the html to call of the respective header and footer components. 我创建了common-header.componentcommon-footer.component来包装html以调用相应的页眉和页脚组件。

How can I call and display the content from these common header and footer feature modules on every page? 如何在每个页面上调用和显示这些通用的页眉和页脚功能模块的内容?

It doesn't matter what module the components are in. Just ensure you have the components in exports of the feature module and the feature modules in imports of the modules where you want to use the components (and directives and pipes) 组件位于哪个模块中都没有关系。只需确保要在其中使用组件(以及指令和管道)的功能模块的导出中包含组件,而在功能模块的导入中具有功能模块。

@NgModule({
  exports: [DirectiveA, PipeB, ComponentC],
  declarations: [DirectiveA, PipeB, ComponentC]
})
class MySharedModule{}
@NgModule({
  imports: [BrowserModule, MySharedModule],
  declarations: [AppComponent],
  declarations: [Bootstrap],
})
class AppModule{}

and add the shared module to imports this way in whatever module you want to reuse directives or pipes of this module. 并将共享模块添加到要重用此模块的指令或管道的任何模块中的imports方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM