模块

在html中引入type='module'的script

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script type="module" src="main.js"></script>
<script type="module" src="message.js"></script>
</body>
</html>
//message.js
function sendMsg() {
    console.log('hello')
}
const msg = 'this is msg '
export {
    sendMsg,
    msg
}
//main.js
//第1种import方式
import {msg,sendMsg} from "./message.js";
console.log(msg)  //this is msg
sendMsg()    //hello
//第2种import方式
import * as file from "./message.js";
console.log(file.msg)
file.sendMsg()

注意:import路径要写完整,跟webpack和node有差别

// 被支持的几种路径写法
import module from 'http://XXX/module.js'
import module from '/XXX/module.js'
import module from './XXX/module.js'
import module from '../XXX/module.js'
// 不被支持的写法
import module from 'XXX'
import module from 'XXX/module.js'
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容