ES6导入模块,导出模块

2020-02-26  本文已影响0人  我性本傲
导出模块
// 单独导出
export let a = 1

// 批量导出
let b = 2
let c = 3
export { b, c }

// 导出接口
export interface P {
    x: number;
    y: number;
}

// 导出函数
export function f() {}

// 导出时起别名
function g() {}
export { g as G }

// 默认导出,无需函数名
export default function () {
    console.log("I'm default")
}

// 引入外部模块,重新导出
export { str as hello } from './b'

导入模块
import { a, b, c } from './a'; // 批量导入
import { P } from './a';       // 导入接口
import { f as F } from './a';  // 导入时起别名
import * as All from './a';    // 导入模块中的所有成员,绑定在 All 上
import myFunction from './a';  // 不加{},导入默认

console.log(a, b, c)

let p: P = {
    x: 1,
    y: 1
}

console.log(All)

myFunction()
上一篇下一篇

猜你喜欢

热点阅读