网站首页 > 教程分享 正文
对导出的模块进行重命名
export { moduleA as mA } from './xxx'
对导入的模块进行重命名
import { moduleB as mB } from './xxx'
let mb = new mB()
将整个模块导入到一个变量,并通过它来访问模块的导出部分
import * as validator from './xxx'
let myValidator = new validator.valida()
若使用export =导出一个模块,则必须使用TypeScript的特定语法import module = require("module")来导入此模块
// ZipCodeValidator.ts
let numberRegexp = /^[0-9]+$/;
class ZipCodeValidator {
isAcceptable(s: string) {
return s.length === 5 && numberRegexp.test(s);
}
}
export = ZipCodeValidator;
// Test.ts
import zip = require("./ZipCodeValidator");
let validator = new zip()
let strings = ["Hello", "98052", "101"];
strings.forEach(s => {
console.log(`"${ s }" - ${ validator.isAcceptable(s) ? "matches" : "does not match" }`);
});
使用注意事项:
如果仅导出单个 class 或 function,使用 export default
如果要导出多个对象,把它们放在顶层里导出
// a.ts
export class SomeType { /* ... */ }
export function someFunc() { /* ... */ }
// b.ts
import { SomeType, someFunc } from "./MyThings";
let x = new SomeType();
let y = someFunc();
当你要导出大量内容的时候,使用命名空间导入模式
// a.ts
export class Dog { ... }
export class Cat { ... }
export class Tree { ... }
export class Flower { ... }
// b.ts
import * as mA from "./a.ts";
let x = new mA.Dog();
let y = new mA.Tree();
猜你喜欢
- 2024-09-09 彻底告别python第三方包import问题!只需要7行代码(码住)
- 2024-09-09 python每日一练之如何copy(python copy)
- 2024-09-09 你知道import Vue from 'vue' 吗?
- 2024-09-09 spring中的@import/@ImportResource注解
- 2024-09-09 Spring中@Import注解详解(spring import注解原理)
- 2024-09-09 python首行代码import *,from * import * 解析
- 2024-09-09 Import注解的理解(java @import注解)
- 2024-09-09 link与@import的区别和分别对应的例子
- 2024-09-09 第五章 import导入第三方库或者模块
- 2024-09-09 ES6模块化一缕(export、import)(es6模块化引入)
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- css导航条 (66)
- sqlinsert (63)
- js提交表单 (60)
- param (62)
- parentelement (65)
- jquery分享 (62)
- check约束 (64)
- curl_init (68)
- sql if语句 (69)
- import (66)
- chmod文件夹 (71)
- clearinterval (71)
- pythonrange (62)
- 数组长度 (61)
- javafx (59)
- 全局消息钩子 (64)
- sort排序 (62)
- jdbc (69)
- php网页源码 (59)
- assert h (69)
- httpclientjar (60)
- postgresql conf (59)
- winform开发 (59)
- mysql数字类型 (71)
- drawimage (61)
本文暂时没有评论,来添加一个吧(●'◡'●)