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

  • Essential Angular
  • Victor Savkin Jeff Cross
  • 231字
  • 2021-07-02 22:56:27

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.
主站蜘蛛池模板: 股票| 奇台县| 灵璧县| 罗城| 东乡族自治县| 乌兰浩特市| 太湖县| 延寿县| 江西省| 湘乡市| 沭阳县| 嵊州市| 淮北市| 苏尼特右旗| 苍南县| 阿合奇县| 华蓥市| 龙海市| 咸阳市| 大化| 仙居县| 开平市| 天峻县| 漳平市| 钟山县| 静乐县| 铜陵市| 建宁县| 绍兴市| 溆浦县| 府谷县| 呼和浩特市| 枣庄市| 谢通门县| 浦县| 合江县| 武清区| 体育| 平山县| 环江| 舟曲县|