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

Multiple exports

So far, we have only shown how to export one construct. It is possible to export multiple things from one module by adding an export keyword next to all constructs that you wish to export, like so:

export class Math {
add() {}
subtract() {}
}

export const PI = 3.14

Essentially, for everything you want to make public you need to add an export keyword at the start of it. There is an alternate syntax, where instead of adding an export keyword to every construct, we can instead define within curly brackets what constructs should be exported. It looks like this:

class Math {
add() {}
subtract() {}
}

const PI = 3.14

export {
Math, PI
}

Whether you put export in front of every construct or you place them all in an export {}, then end result is the same, it's just a matter of taste which one to use. To consume constructs from this module, we would type: 

import { Math, PI } from './module';

Here, we have the option of specifying what we want to import. In the previous example, we have opted to export both Math and PI, but we could be content with only exporting Math, for example; it is up to us.

主站蜘蛛池模板: 安宁市| 囊谦县| 平遥县| 长海县| 温泉县| 惠水县| 汉寿县| 江西省| 宝坻区| 巫山县| 伽师县| 原平市| 绩溪县| 五家渠市| 郸城县| 吐鲁番市| 鄂伦春自治旗| 阿坝| 海南省| 三都| 长阳| 苏尼特右旗| 合江县| 华蓥市| 赤峰市| 桐梓县| 雅江县| 陆良县| 息烽县| 临沂市| 博兴县| 竹溪县| 玉溪市| 双牌县| 晋宁县| 普兰县| 独山县| 上思县| 桓台县| 滕州市| 双流县|