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

  • 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.

主站蜘蛛池模板: 太谷县| 富阳市| 黄平县| 正镶白旗| 车致| 长寿区| 潍坊市| 谢通门县| 卓资县| 景德镇市| 仙游县| 巴中市| 防城港市| 泰州市| 麻江县| 延边| 敦化市| 泾川县| 兰西县| 庆云县| 桓仁| 拜泉县| 大余县| 英山县| 鄂托克旗| 海安县| 松原市| 兰西县| 临泉县| 濮阳县| 汨罗市| 荔浦县| 岑溪市| 兖州市| 集安市| 黄浦区| 滦平县| 泽普县| 岳阳市| 三都| 清水县|