以下是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 的導出!