首页 > 其他分享 >兼收并蓄 TypeScript - 类: 模块

兼收并蓄 TypeScript - 类: 模块

时间:2024-09-20 12:28:38浏览次数:1  
标签:TypeScript name module 兼收并蓄 export 模块 import hello

源码 https://github.com/webabcd/TypeScriptDemo
作者 webabcd

兼收并蓄 TypeScript - 类: 模块

示例如下:

module\main.ts

/**
 * 本例用于演示 import, export
 */

// 从指定的模块中导入指定的被 export 的变量或函数或对象
import { name, hello } from './a';
// 从指定的模块中导入指定的被 export 的变量或函数或对象
import { name_b, hello_b } from './b';
// 从指定的模块中导入指定的被 export 的变量或函数或对象(并重命名)
import { name as name_c, hello as hello_c } from './c';
// 从指定的模块中导入指定的被 export 的变量或函数或对象(并重命名)
import { name as name_d, hello as hello_d } from './d';
// 从指定的模块中导入被 export default 的变量或函数或对象,并为其命名
// 注:对于 export default 导出的对象,在 import 的时候不需要加 {}
import obj_e from './e';
// 从指定的模块中导入被 export default 的变量或函数或对象,并为其命名
// 注:对于 export default 导出的对象,在 import 的时候不需要加 {}
import obj_f from './f';
// 从指定的模块中导入指定的被 export 的变量或函数或对象
import { name as name_g, hello as hello_g }  from './g';
// 从指定的模块中导入被 export 的全部内容
import * as obj_h from './h';

console.log(`a: ${name} ${hello()}`);
console.log(`b: ${name_b} ${hello_b()}`);
console.log(`c: ${name_c} ${hello_c()}`);
console.log(`d: ${name_d} ${hello_d()}`);
console.log(`e: ${obj_e.name} ${obj_e.hello()}`);
console.log(`f: ${obj_f.name} ${obj_f.hello()}`);
console.log(`g: ${name_g} ${hello_g()}`);
console.log(`h: ${obj_h.name} ${obj_h.hello()}`);

// 将 './i' 文件导入并编译
import './i';

module\dynamicImport.ts

/**
 * 本例用于演示模块的动态导入(es2020 新特性)
 */

setTimeout(async () => { 
    // 模块的动态导入
    const a = await import('./a.js');
    console.log(`${a.name} ${a.hello()}`); // name hello
}, 3000);

module\a.ts

let name = "name";
let hello = function() {
    return "hello"
};

// 导出指定的变量或函数或对象
export { name, hello }

module\b.ts

let name = "name";
let hello = function() {
    return "hello"
};

// 导出指定的变量或函数或对象(并重命名)
export { name as name_b, hello as hello_b};

module\c.ts

// 导出指定的变量或函数或对象
export let name = "name", hello = ()=> { return "hello"; };

module\d.ts

// 导出指定的变量或函数或对象
// 一个模块中可以有多个 export
export let name = "name";
export let hello = ()=> { return "hello"; };

module\e.ts

// 导出 default 变量或函数或对象
// 一个模块中只能有一个 export default
export default {
    name : "name",
    hello:function() {
        return "hello";
    }
}

module\f.ts

let obj = {
    name : "name",
    hello:function() {
        return "hello";
    }
}

// 导出 default 变量或函数或对象
// 一个模块中只能有一个 export default
export default obj;

module\g.ts

// 从指定的模块中导出全部内容
export * from './a';

module\h.ts

let name = "name";
let hello = function() {
    return "hello"
};

// 导出指定的变量或函数或对象
export { name, hello }

module\i.ts

let myName = "i am webabcd";
console.log(myName);

源码 https://github.com/webabcd/TypeScriptDemo
作者 webabcd

标签:TypeScript,name,module,兼收并蓄,export,模块,import,hello
From: https://www.cnblogs.com/webabcd/p/18422266/typescript_module_main

相关文章

  • 兼收并蓄 TypeScript - 进阶: ArrayBuffer
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-进阶:ArrayBuffer示例如下:advanced\arrayBuffer.ts{/***1、ArrayBuffer-内存之中的一段二进制数据,需要通过视图操作数据*2、TypedArray-视图,用于操作ArrayBuf......
  • 兼收并蓄 TypeScript - 进阶: promise
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-进阶:promise示例如下:advanced\promise.ts{/***Promise-用于异步编程(非多线程)*有3种状态:pending(进行中),fulfilled(已成功),rejected(已失败)*状态只能从......
  • 兼收并蓄 TypeScript - 进阶: async/await
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-进阶:async/await示例如下:advanced\async_await.ts{/***async/await-用于异步编程(非多线程)*asyncfunction返回的是Promise对象*await用于等Pro......
  • 兼收并蓄 TypeScript - 进阶: iterator, generator
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-进阶:iterator,generator示例如下:advanced\iterator_generator.ts{/***iterator-迭代器(可迭代对象有Array,TypedArray,Map,Set,String)*next()-迭代到......
  • 兼收并蓄 TypeScript - 进阶: proxy, reflect
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-进阶:proxy,reflect示例如下:advanced\proxy_reflect.ts{//Proxy-代理(拦截目标对象的属性操作和函数操作)lettarget={name:'webabcd',age:40,......
  • 兼收并蓄 TypeScript - 第三方库: 类型声明
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-第三方库:类型声明示例如下:third\typeDeclaration.ts/**类型声明用于TypeScript调用JavaScript*类型声明定义在.d.ts声明文件中*比如aes.js文件,其对应的声明文件为ae......
  • 兼收并蓄 TypeScript - 第三方库: crypto-js
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-第三方库:crypto-js示例如下:third\cryptojs.ts/**本例以在TypeScript中使用crypto-js为例*crypto-js是一个纯js项目,是不能直接在typescript中使用的,需要相应的.d.ts......
  • 兼收并蓄 TypeScript - 基础: number
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-基础:number示例如下:basic\number.ts{//将指定类型的数据转换为number类型console.log(Number("100"),Number(true),Number({}),Number([]));//1001NaN0//......
  • 兼收并蓄 TypeScript - 基础: string
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-基础:string示例如下:basic\string.ts{leta="\x7A";//十六进制的“7A”是字符“z”letb="\u{7A}";//十六进制的“7A”是字符“z”letc="\u{738B}";......
  • 兼收并蓄 TypeScript - 基础: symbol
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-基础:symbol示例如下:basic\symbol.ts{//Symbol()是一个全局函数,返回一个唯一的Symbol值consta=Symbol();constb=Symbol();//b与a不同constc=Sy......