导入JavaScript的全部内容
<script src="JavaScriptName.js"></script>
逐个导出
JS代码
export function getMessage(msg){console.log(msg)
}
HTML代码
<script type="module">import { getMessage } from './showMessage.js'document.getElementById("btn").onclick = function(){getMessage('123');}
</script>
批量导出
JS代码
function complexMessage(msg){console.log(new Date()+": "+msg)
}export {complexMessage}
HTML代码
<body><div><button id="btn" >点我展示信息</button></div><script type="module">import { complexMessage } from './showMessage.js'document.getElementById("btn").onclick = function(){complexMessage('bbbbb');}</script>
</body>
默认批量导出
JS代码
//复杂的展示信息
function complexMessage(msg){console.log(new Date()+": "+msg)
}export default {complexMessage}
HTML代码
<body><div><button id="btn" >点我展示信息</button></div><script type="module">import messageMethods from './showMessage.js’messageMethods.complexMessage('bbb');</script>
</body>
设置别名
导入或者导出时,可以用as给方法设置别名
- 导入时设置:
<script type="module">import { getMessage as gm } from './showMessage.js'document.getElementById("btn").onclick = function(){gm('123');}
</script>
- 导出时设置
function complexMessage(msg){console.log(new Date()+": "+msg)
}export {complexMessage as cm}
笔记参考