1、TypeScript 模塊
TypeScript 模塊的設(shè)計理念是可以更換的組織代碼。
模塊是在其自身的作用域里執(zhí)行,并不是在全局作用域,這意味著定義在模塊里面的變量、函數(shù)和類等在模塊外部是不可見的,除非明確地使用 export 導(dǎo)出它們。類似地,我們必須通過 import 導(dǎo)入其他模塊導(dǎo)出的變量、函數(shù)、類等。
兩個模塊之間的關(guān)系是通過在文件級別上使用 import 和 export 建立的。
模塊使用模塊加載器去導(dǎo)入其它的模塊。 在運行時,模塊加載器的作用是在執(zhí)行此模塊代碼前去查找并執(zhí)行這個模塊的所有依賴。 大家最熟知的JavaScript模塊加載器是服務(wù)于 Node.js 的 CommonJS 和服務(wù)于 Web 應(yīng)用的 Require.js。
此外還有有 SystemJs 和 Webpack。
模塊導(dǎo)出使用關(guān)鍵字 export 關(guān)鍵字,語法格式如下:
// 文件名 : SomeInterface.ts
export interface SomeInterface {
// 代碼部分
}復(fù)制
要在另外一個文件使用該模塊就需要使用 import 關(guān)鍵字來導(dǎo)入:
import someInterfaceRef = require("./SomeInterface");復(fù)制
實例
鴻蒙開發(fā)文檔指導(dǎo):[qr23.cn/AKFP8k
]
IShape.ts 文件代碼:
/// < reference path = "IShape.ts" / >
export interface IShape {
draw();
}復(fù)制
Circle.ts 文件代碼:
import shape = require("./IShape");
export class Circle implements shape.IShape {
public draw() {
console.log("Cirlce is drawn (external module)");
}
}復(fù)制
Triangle.ts 文件代碼:
import shape = require("./IShape");
export class Triangle implements shape.IShape {
public draw() {
console.log("Triangle is drawn (external module)");
}
}復(fù)制
TestShape.ts 文件代碼:
import shape = require("./IShape");
import circle = require("./Circle");
import triangle = require("./Triangle");
function drawAllShapes(shapeToDraw: shape.IShape) {
shapeToDraw.draw();
}
drawAllShapes(new circle.Circle());
drawAllShapes(new triangle.Triangle());復(fù)制
使用 tsc 命令編譯以上代碼(AMD):
tsc --module amd TestShape.ts
得到以下 JavaScript 代碼:
IShape.js 文件代碼:
define(["require", "exports"], function (require, exports) { });
Circle.js 文件代碼:
define(["require", "exports"], function (require, exports) {
var Circle = (function () {
function Circle() {
}
Circle.prototype.draw = function () {
console.log("Cirlce is drawn (external module)");
};
return Circle;
})();
exports.Circle = Circle;
});復(fù)制
Triangle.js 文件代碼:
define(["require", "exports"], function (require, exports) {
var Triangle = (function () {
function Triangle() {
}
Triangle.prototype.draw = function () {
console.log("Triangle is drawn (external module)");
};
return Triangle;
})();
exports.Triangle = Triangle;
});復(fù)制
TestShape.js 文件代碼:
define(["require", "exports", "./Circle", "./Triangle"],
function (require, exports, circle, triangle) {
function drawAllShapes(shapeToDraw) {
shapeToDraw.draw();
}
drawAllShapes(new circle.Circle());
drawAllShapes(new triangle.Triangle());
});復(fù)制
使用 tsc 命令編譯以上代碼(Commonjs):
tsc --module commonjs TestShape.ts
得到以下 JavaScript 代碼:
Circle.js 文件代碼:
var Circle = (function () {
function Circle() {
}
Circle.prototype.draw = function () {
console.log("Cirlce is drawn");
};
return Circle;
})();
exports.Circle = Circle;復(fù)制
Triangle.js 文件代碼:
var Triangle = (function () {
function Triangle() {
}
Triangle.prototype.draw = function () {
console.log("Triangle is drawn (external module)");
};
return Triangle;
})();
exports.Triangle = Triangle;復(fù)制
TestShape.js 文件代碼:
var circle = require("./Circle");
var triangle = require("./Triangle");
function drawAllShapes(shapeToDraw) {
shapeToDraw.draw();
}
drawAllShapes(new circle.Circle());
drawAllShapes(new triangle.Triangle());復(fù)制
輸出結(jié)果為:
Cirlce is drawn (external module)
Triangle is drawn (external module)
審核編輯 黃宇
-
鴻蒙
+關(guān)注
關(guān)注
57文章
2392瀏覽量
43050
發(fā)布評論請先 登錄
相關(guān)推薦
鴻蒙TypeScript入門學(xué)習(xí)第4天:【TS變量聲明】
鴻蒙TypeScript入門學(xué)習(xí)第6天:【條件語句】
![<b class='flag-5'>鴻蒙</b><b class='flag-5'>TypeScript</b>入門<b class='flag-5'>學(xué)習(xí)</b><b class='flag-5'>第</b>6<b class='flag-5'>天</b>:【條件語句】](https://file1.elecfans.com/web2/M00/C6/84/wKgZomYKR_aAbRPhAABW3F6g280594.png)
鴻蒙TypeScript學(xué)習(xí)第7天:【TypeScript 循環(huán)】
![<b class='flag-5'>鴻蒙</b><b class='flag-5'>TypeScript</b><b class='flag-5'>學(xué)習(xí)</b><b class='flag-5'>第</b>7<b class='flag-5'>天</b>:【<b class='flag-5'>TypeScript</b> 循環(huán)】](https://file1.elecfans.com/web2/M00/C7/8F/wKgaomYLpDmAT_0IAABO30lwptw557.jpg)
鴻蒙TypeScript 開發(fā)學(xué)習(xí)第9天:【TypeScript Number】
![<b class='flag-5'>鴻蒙</b><b class='flag-5'>TypeScript</b> 開發(fā)<b class='flag-5'>學(xué)習(xí)</b><b class='flag-5'>第</b>9<b class='flag-5'>天</b>:【<b class='flag-5'>TypeScript</b> Number】](https://file1.elecfans.com/web2/M00/C5/CD/wKgZomYCdwyAIFf5AAB_7E1pFms943.jpg)
【觸覺智能 Purple Pi OH 開發(fā)板體驗】二、鴻蒙系統(tǒng)APP應(yīng)用例程學(xué)習(xí)HDC使用學(xué)習(xí)
鴻蒙TypeScript入門學(xué)習(xí)第2天【TypeScript安裝】
![<b class='flag-5'>鴻蒙</b><b class='flag-5'>TypeScript</b>入門<b class='flag-5'>學(xué)習(xí)</b><b class='flag-5'>第</b>2<b class='flag-5'>天</b>【<b class='flag-5'>TypeScript</b>安裝】](https://file1.elecfans.com/web2/M00/C6/DA/wKgaomYDxy2APS8FAABixqJ8Axg195.jpg)
鴻蒙TypeScript開發(fā)入門學(xué)習(xí)第3天:【TS基礎(chǔ)類型】
![<b class='flag-5'>鴻蒙</b><b class='flag-5'>TypeScript</b>開發(fā)入門<b class='flag-5'>學(xué)習(xí)</b><b class='flag-5'>第</b>3<b class='flag-5'>天</b>:【TS基礎(chǔ)類型】](https://file1.elecfans.com/web2/M00/C6/14/wKgZomYFEn-AfD6fAACtgCysfDU514.jpg)
鴻蒙TypeScript入門學(xué)習(xí)第8天:【TypeScript 函數(shù)】
![<b class='flag-5'>鴻蒙</b><b class='flag-5'>TypeScript</b>入門<b class='flag-5'>學(xué)習(xí)</b><b class='flag-5'>第</b>8<b class='flag-5'>天</b>:【<b class='flag-5'>TypeScript</b> 函數(shù)】](https://file1.elecfans.com/web2/M00/C5/CD/wKgZomYCdwyAIFf5AAB_7E1pFms943.jpg)
鴻蒙TypeScript學(xué)習(xí)21天:【聲明文件】
![<b class='flag-5'>鴻蒙</b><b class='flag-5'>TypeScript</b><b class='flag-5'>學(xué)習(xí)</b>21<b class='flag-5'>天</b>:【聲明文件】](https://file1.elecfans.com/web2/M00/C6/C5/wKgaomYCyYKAZp6HAAB4LWPdpdQ014.jpg)
評論