前言:
现在你们对“js导出函数”大致比较重视,我们都想要了解一些“js导出函数”的相关内容。那么小编也在网上收集了一些有关“js导出函数””的相关内容,希望看官们能喜欢,兄弟们一起来学习一下吧!1.js导入导出
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>js导入导出</title></head><body> <div> <button id="btn" >点我展示信息</button> </div> <!-- 导入showMessage.js文件的全部内容 --> <script src="showMessage.js"></script> <script> document.getElementById("btn").onclick = function(){ complexMessage('bbbbb'); }</script></body></html>
js
//简单的展示信息function simpleMessage(msg){ console.log(msg)}//复杂的展示信息function complexMessage(msg){ console.log(new Date()+": "+msg)}
//简单的展示信息
export function simpleMessage(msg){
console.log(msg)
}
//复杂的展示信息
export function complexMessage(msg){
console.log(new Date()+": "+msg)
}
export命令可以按需导出,如果需要批量导出则可以添加
export{ simpleMessage,complexMessage};
导出时可以用别名
import { messageMethods as cm } from './showMessage.js’
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>js导入导出</title></head><body> <div> <button id="btn" >点我展示信息</button> </div> <script type="module"> import { complexMessage as cm } from "./showMessage.js"; document.getElementById("btn").onclick = function(){ cm('bbbbb'); }</script></body></html>
//简单的展示信息 function simpleMessage(msg){ console.log(msg)}//复杂的展示信息 function complexMessage(msg){ console.log(new Date()+": "+msg)}export{ simpleMessage ,complexMessage }
默认导出
//简单的展示信息export function simpleMessage(msg){ console.log(msg)}//复杂的展示信息export function complexMessage(msg){ console.log(new Date()+": "+msg)}export default { simpleMessage ,complexMessage }
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>js导入导出</title></head><body> <div> <button id="btn" >点我展示信息</button> </div> <script type="module"> import xx from "./showMessage.js"; document.getElementById("btn").onclick = function(){ xx.complexMessage('bbbbb'); }</script></body></html>
使用默认导出后,那么xx.可以带出函数。
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。