非vue等框架中html 中使用es6的模塊用法小結


以下是html中使用es6模塊化引入的方法

一、html中的引入

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div>1212</div>
    <script type="module">
        import {add as newadd} from './b.js'
        let res = newadd(2,3)
        console.log(res)
    </script>
</body>

</html>

注意:要加上 type="module"  !!!並且配置好本地服務器訪問html 。

如果是npm 下載后的包(node_modules),則貌似無法直接通過import ‘引入’ ,需要webpack的配置??

 

二、js文件中的寫法:

function add(a, b) {
    return a + b + 2;
}
export { add };

 

注意:建議js的導出利用export 的導出,而非export default 的導出!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM