- Switching to Angular(Third Edition)
- Minko Gechev
- 311字
- 2021-07-02 15:23:33
Angular modules
In AngularJS, we have the concept of modules. Modules there are responsible for grouping pieces of related functionality together and registering it internally during the bootstrap process. Unfortunately, they didn't provide features such as encapsulation and lazy loading.
Angular introduced the NgModules as part of the fifth release candidate of the framework. The main purpose of the new modules is to give a context for the Angular compiler and achieve a good level of encapsulation. For instance, if we are building a library with NgModules, we can have a number of declarations, which are used internally but not exported as part of the public interface. Let's take a look at the following example:
import {NgModule} from '@angular/core'; import {CommonModule} from '@angular/common'; import {TabComponent} from './tab.component'; import {TabItemComponent} from './tab-item.component'; @NgModule({ imports: [CommonModule], declarations: [TabComponent, TabItemComponent], exports: [TabComponent] }) class TabModule { }
Do not worry if you're not familiar with the TypeScript syntax in the preceding example; we will take a deep dive into the language in the next chapter.
In the preceding code snippet, using the @NgModule decorator, we declare TabModule. Note that in the list of declarations, we include both TabComponent and TabItemComponent, but in the list of exports, we have only TabComponent. This way, we can achieve a level of encapsulation for our library. The users of the module will be able to use only TabComponent, so we don't have to worry about backward compatibility of the API of TabItemComponent since it's accessible only internally, within the boundaries of our module. Finally, by setting the imports property of the object literal passed to @NgModule, we can list modules that we want to use inside of the current module. This way, we will be able to take advantage of all the exports and providers (we'll discuss providers in Chapter 6, Dependency Injection in Angular) declared by them.
- iOS面試一戰(zhàn)到底
- 大學(xué)計(jì)算機(jī)基礎(chǔ)實(shí)驗(yàn)教程
- 跟“龍哥”學(xué)C語(yǔ)言編程
- Mastering QGIS
- Visual Basic學(xué)習(xí)手冊(cè)
- Building a Quadcopter with Arduino
- Mastering JBoss Enterprise Application Platform 7
- Android項(xiàng)目實(shí)戰(zhàn):手機(jī)安全衛(wèi)士開發(fā)案例解析
- 時(shí)空數(shù)據(jù)建模及其應(yīng)用
- UML2面向?qū)ο蠓治雠c設(shè)計(jì)(第2版)
- 算法超簡(jiǎn)單:趣味游戲帶你輕松入門與實(shí)踐
- Responsive Web Design with jQuery
- Python繪圖指南:分形與數(shù)據(jù)可視化(全彩)
- Learning Apache Thrift
- Learning Zimbra Server Essentials