重庆展示型网站制作,计算机编程入门,潍坊网站制作,制作一个网站都需要学什么1、TypeScript模块#xff1a;
模块是在其自身的作用域里执行#xff0c;并不是在全局作用域#xff0c;这意味着定义在模块里面的变量、函数和类等在模块外部是不可见的#xff0c;除非明确地使用 export 导出它们。类似地#xff0c;我们必须通过 import 导入其他模块导…
1、TypeScript模块
模块是在其自身的作用域里执行并不是在全局作用域这意味着定义在模块里面的变量、函数和类等在模块外部是不可见的除非明确地使用 export 导出它们。类似地我们必须通过 import 导入其他模块导出的变量、函数、类等。
两个模块之间的关系是通过在文件级别上使用 import 和 export 建立的。
2、模块导出使用关键字export 关键字语法格式
// 文件名 : SomeInterface.ts
export interface SomeInterface { // 代码部分
}
要在另外一个文件使用该模块就需要使用 import 关键字来导入:
import someInterfaceRef require(./SomeInterface);
实例
IShape.ts 文件代码
/// reference path IShape.ts /
export interface IShape { draw();
}
Circle.ts 文件代码
import shape require(./IShape);
export class Circle implements shape.IShape { public draw() { console.log(Cirlce is drawn (external module)); }
}
Triangle.ts 文件代码
import shape require(./IShape);
export class Triangle implements shape.IShape { public draw() { console.log(Triangle is drawn (external module)); }
}
estShape.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());
执行结果 Cirlce is drawn (external module) Triangle is drawn (external module)