- 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.
- 劍指JVM:虛擬機實踐與性能調優
- 深入淺出Prometheus:原理、應用、源碼與拓展詳解
- R語言數據可視化實戰
- 假如C語言是我發明的:講給孩子聽的大師編程課
- Java EE 7 Development with NetBeans 8
- Java應用開發技術實例教程
- .NET 3.5編程
- 快人一步:系統性能提高之道
- Mastering JavaScript High Performance
- 區塊鏈技術進階與實戰(第2版)
- Julia for Data Science
- Python數據可視化之美:專業圖表繪制指南(全彩)
- 編程的原則:改善代碼質量的101個方法
- SCRATCH編程課:我的游戲我做主
- HikariCP數據庫連接池實戰