es6 module

97 阅读1分钟

之前以为浏览器不支持es module 也是需要label或webpack才能使用的,原来是使用方式不对

// 测试:模块a,需要import 模块b
  • 加载模块a
    <script src="./a.js" type="module"></script>
    // 需要使用module标识,浏览器才会识别import
  • a模块中import b
// 测试1
import {b} from './b.js'
console.log(b)

// 测试2
setTimeout(() => {
    import('./b.js').then(res => {
        console.log('res',res)
    })   
}, 5000);
  • 完成