0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

鴻蒙TypeScript學(xué)習(xí)第20天:【模塊】

jf_46214456 ? 來源:jf_46214456 ? 作者:jf_46214456 ? 2024-04-18 15:19 ? 次閱讀

1、TypeScript 模塊

TypeScript 模塊的設(shè)計(jì)理念是可以更換的組織代碼。

模塊是在其自身的作用域里執(zhí)行,并不是在全局作用域,這意味著定義在模塊里面的變量、函數(shù)和類等在模塊外部是不可見的,除非明確地使用 export 導(dǎo)出它們。類似地,我們必須通過 import 導(dǎo)入其他模塊導(dǎo)出的變量、函數(shù)、類等。

兩個(gè)模塊之間的關(guān)系是通過在文件級別上使用 import 和 export 建立的。

模塊使用模塊加載器去導(dǎo)入其它的模塊。 在運(yùn)行時(shí),模塊加載器的作用是在執(zhí)行此模塊代碼前去查找并執(zhí)行這個(gè)模塊的所有依賴。 大家最熟知的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ù)制

要在另外一個(gè)文件使用該模塊就需要使用 import 關(guān)鍵字來導(dǎo)入:

import someInterfaceRef = require("./SomeInterface");復(fù)制

實(shí)例

鴻蒙開發(fā)文檔指導(dǎo):[qr23.cn/AKFP8k]

搜狗高速瀏覽器截圖20240326151450.png

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)

審核編輯 黃宇

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報(bào)投訴
  • 鴻蒙
    +關(guān)注

    關(guān)注

    56

    文章

    2267

    瀏覽量

    42481
收藏 人收藏

    評論

    相關(guān)推薦

    鴻蒙TypeScript入門學(xué)習(xí)4:【TS變量聲明】

    變量是一種使用方便的占位符,用于引用計(jì)算機(jī)內(nèi)存地址。 我們可以把變量看做存儲(chǔ)數(shù)據(jù)的容器。
    的頭像 發(fā)表于 03-29 14:49 ?1222次閱讀

    鴻蒙TypeScript入門學(xué)習(xí)6:【條件語句】

    條件語句用于基于不同的條件來執(zhí)行不同的動(dòng)作。 TypeScript 條件語句是通過一條或多條語句的執(zhí)行結(jié)果(True 或 False)來決定執(zhí)行的代碼塊。
    的頭像 發(fā)表于 04-01 13:51 ?637次閱讀
    <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>:【條件語句】

    鴻蒙TypeScript學(xué)習(xí)7:【TypeScript 循環(huán)】

    有的時(shí)候,我們可能需要多次執(zhí)行同一塊代碼。一般情況下,語句是按順序執(zhí)行的:函數(shù)中的第一個(gè)語句先執(zhí)行,接著是第二個(gè)語句,依此類推。 編程語言提供了更為復(fù)雜執(zhí)行路徑的多種控制結(jié)構(gòu)。
    的頭像 發(fā)表于 04-02 14:28 ?700次閱讀
    <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)】

    鴻蒙TypeScript 開發(fā)學(xué)習(xí)9:【TypeScript Number】

    TypeScript 與 JavaScript 類似,支持 Number 對象。 Number 對象是原始數(shù)值的包裝對象。
    的頭像 發(fā)表于 04-07 18:02 ?672次閱讀
    <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】

    鴻蒙TypeScript入門學(xué)習(xí)11【Array(數(shù)組)】

    數(shù)組對象是使用單獨(dú)的變量名來存儲(chǔ)一系列的值。 數(shù)組非常常用。
    的頭像 發(fā)表于 04-09 14:38 ?891次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>TypeScript</b>入門<b class='flag-5'>學(xué)習(xí)</b><b class='flag-5'>第</b>11<b class='flag-5'>天</b>【Array(數(shù)組)】

    鴻蒙語言TypeScript學(xué)習(xí)16:【類】

    TypeScript 支持面向?qū)ο蟮乃刑匦?,比?類、接口等。
    的頭像 發(fā)表于 04-15 09:29 ?811次閱讀
    <b class='flag-5'>鴻蒙</b>語言<b class='flag-5'>TypeScript</b><b class='flag-5'>學(xué)習(xí)</b><b class='flag-5'>第</b>16<b class='flag-5'>天</b>:【類】

    鴻蒙TypeScript學(xué)習(xí)17:【對象】

    對象是包含一組鍵值對的實(shí)例。 值可以是標(biāo)量、函數(shù)、數(shù)組、對象等
    的頭像 發(fā)表于 04-15 15:33 ?549次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>TypeScript</b><b class='flag-5'>學(xué)習(xí)</b><b class='flag-5'>第</b>17<b class='flag-5'>天</b>:【對象】

    【觸覺智能 Purple Pi OH 開發(fā)板體驗(yàn)】二、鴻蒙系統(tǒng)APP應(yīng)用例程學(xué)習(xí)HDC使用學(xué)習(xí)

    兩年開發(fā)鴻蒙APP也是使用的這兩種語言進(jìn)行開發(fā)。當(dāng)下看TypeScript程序還能說勉強(qiáng)看懂,但是當(dāng)下開發(fā)程序就沒那個(gè)實(shí)力了,需要之后在抽時(shí)間學(xué)習(xí)。技術(shù)更新的也確實(shí)是快?。。。?二、天氣預(yù)報(bào)程序開發(fā)
    發(fā)表于 08-31 11:13

    鴻蒙TypeScript入門學(xué)習(xí)2TypeScript安裝】

    本文介紹 TypeScript 環(huán)境的安裝。 我們需要使用到 npm 工具安裝,如果你還不了解 npm,可以參考我之前文檔。
    的頭像 發(fā)表于 03-27 15:22 ?380次閱讀
    <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>安裝】

    鴻蒙TypeScript開發(fā)入門學(xué)習(xí)3:【TS基礎(chǔ)類型】

    任意值是 TypeScript 針對編程時(shí)類型不明確的變量使用的一種數(shù)據(jù)類型,它常用于以下三種情況。
    的頭像 發(fā)表于 03-28 15:02 ?391次閱讀
    <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ǔ)類型】

    鴻蒙TypeScript入門學(xué)習(xí)8:【TypeScript 函數(shù)】

    函數(shù)是一組一起執(zhí)行一個(gè)任務(wù)的語句。 您可以把代碼劃分到不同的函數(shù)中。如何劃分代碼到不同的函數(shù)中是由您來決定的,但在邏輯上,劃分通常是根據(jù)每個(gè)函數(shù)執(zhí)行一個(gè)特定的任務(wù)來進(jìn)行的。
    的頭像 發(fā)表于 04-03 14:54 ?322次閱讀
    <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ù)】

    鴻蒙TypeScript學(xué)習(xí)12【Map對象】

    Map 對象保存鍵值對,并且能夠記住鍵的原始插入順序。 任何值(對象或者原始值) 都可以作為一個(gè)鍵或一個(gè)值。
    的頭像 發(fā)表于 04-10 15:47 ?858次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>TypeScript</b><b class='flag-5'>學(xué)習(xí)</b><b class='flag-5'>第</b>12<b class='flag-5'>天</b>【Map對象】

    鴻蒙TypeScript學(xué)習(xí)13:【元組】

    元組中允許存儲(chǔ)不同類型的元素,元組可以作為參數(shù)傳遞給函數(shù)。
    的頭像 發(fā)表于 04-11 14:43 ?327次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>TypeScript</b><b class='flag-5'>學(xué)習(xí)</b><b class='flag-5'>第</b>13<b class='flag-5'>天</b>:【元組】

    鴻蒙語言TypeScript學(xué)習(xí)18:【泛型】

    泛型(Generics)是一種編程語言特性,允許在定義函數(shù)、類、接口等時(shí)使用占位符來表示類型,而不是具體的類型。
    的頭像 發(fā)表于 04-16 14:56 ?281次閱讀
    <b class='flag-5'>鴻蒙</b>語言<b class='flag-5'>TypeScript</b><b class='flag-5'>學(xué)習(xí)</b><b class='flag-5'>第</b>18<b class='flag-5'>天</b>:【泛型】

    鴻蒙TypeScript學(xué)習(xí)21:【聲明文件】

    TypeScript 作為 JavaScript 的超集,在開發(fā)過程中不可避免要引用其他第三方的 JavaScript 的庫。
    的頭像 發(fā)表于 04-19 15:02 ?412次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>TypeScript</b><b class='flag-5'>學(xué)習(xí)</b>21<b class='flag-5'>天</b>:【聲明文件】