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

  • Learning Angular
  • Aristeidis Bampakos Pablo Deeleman
  • 219字
  • 2021-06-11 18:24:05

Modules

As our applications scale and grow in size, there will be a time when we need to organize our code better and make it sustainable and more reusable. Modules are responsible for this need, so let's take a look at how they work and how we can implement them in our application.

A module works at a file level, where each file is the module itself, and the module name matches the filename without the .ts extension. Each member marked with the export keyword becomes part of the module's public API:

my-service.ts

export class MyService {

    getData() {}

}

To use this module and its exported class, we need to import it:

import { MyService } from './my-service';

Notice that the./my-service path is relative to the location of the file that imports the module. If the module exports more than one artifact, we place them inside the curly braces one by one, separated with a comma:

export class MyService {

    getData() {}

}

export const PI = 3.14;

import { MyService, PI } from './my-service'; 

In the preceding example, MyService exports the getData method and the PI variable in one go.

主站蜘蛛池模板: 平乐县| 科技| 揭西县| 武义县| 尚义县| 亚东县| 彭山县| 南昌县| 河间市| 咸宁市| 林西县| 惠东县| 同江市| 酉阳| 松阳县| 固原市| 西安市| 凯里市| 黄山市| 砚山县| 涞水县| 洪江市| 桓仁| 襄汾县| 嵊州市| 团风县| 洪湖市| 乐山市| 齐齐哈尔市| 依安县| 仁化县| 楚雄市| 南木林县| 鞍山市| 福海县| 苍溪县| 花垣县| 明溪县| 邵武市| 马公市| 乡宁县|