js 中 import * as xxx from几种用法

1,706 阅读1分钟
// exoprt.js
function test01(test01) {
    console.log(test01);
}

function test02(test02) {
    console.log(test02);
}


// 出口

// AAA
export {
    test01,
    test02
}

// BBB
export {
    test01 as t1,
    test02 as t2
}

// CCC
export function test01(test01) {
    console.log(test01);
}
export function test02(test02) {
    console.log(test02);
}

--------------------------调用
// html

// AAA1
<script type="module">  // 注意type要写module
    import * as Export from './exoprt.js';
    Export.test01("111");  // 111
</script>
// AAA2
<script type="module">
    import {test01, test02} from './exoprt.js';
    test01("111");  // 111
</script>
// AAA2
<script type="module">
    import {
        test01 as t1, 
        test02 as t2
    } from './exoprt.js';
    t1("111");  // 111
</script>


// 注:
如果导入多个文件有重名的
import {test as test01} from 'js1.js';
import {test as test02} from 'js2.js';