官术网_书友最值得收藏!

NgModules

Declarations, imports, and exports

NgModules are the unit of compilation and distribution of Angular components and pipes. In many ways, they are similar to ES6 modules, in that they have declarations, imports, and exports.

Let's look at this example:

@NgModule({
declarations: [FormattedRatingPipe, WatchButtonCmp, \
RateButtonCmp, TalkCmp, TalksCmp],
exports: [TalksCmp]
})
class TalksModule {}

@NgModule({
declarations: [AppCmp],
imports: [BrowserModule, TalksModule],
bootstrap: [AppCmp]
})
class AppModule {}

Here we define two modules: TalksModule and AppModule. TalksModule has all the components and pipes that do actual work in the application, whereas AppModule has only AppCmp, which is a thin application shell.

TalksModule declares four components and one pipe. The four components can use each other in their templates, similar to how classes defined in an ES module can refer to each other in their methods. Also, all the components can use FormattedRatingPipe. So an NgModule is the compilation context of its components, that is, it tells Angular how these components should be compiled. As with ES, a component can only be declared in one module.

In this example TalksModule exports only TalksCmp—the rest is private. This means that only TalksCmp is added to the compilation context of AppModule. Again this is similar to how the export keyword works in JavaScript.

Summary

  • NgModules are akin to ES modules: they have declarations, imports, and exports.
  • NgModules define the compilation context of their components.
主站蜘蛛池模板: 柳江县| 台山市| 城固县| 绥宁县| 大渡口区| 赣榆县| 合江县| 紫阳县| 神木县| 岗巴县| 舞阳县| 沭阳县| 英超| 麦盖提县| 英超| 岑溪市| 长丰县| 华池县| 华容县| 静海县| 湘阴县| 青铜峡市| 通辽市| 湟源县| 三河市| 峡江县| 和田市| 黔西| 梁山县| 丹棱县| 唐山市| 五寨县| 开原市| 禹城市| 洪湖市| 石棉县| 汨罗市| 宜兰县| 和田市| 商洛市| 南木林县|