esModule的模块加载在普通html中使用
作者:互联网
-
add.js
const add = (x, y) => { return x + y; } export default add;
minus.js
const minus = (x, y) => { return x - y; } export default minus;
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <script type="module"> import add from './module/add.js'; import minus from './module/minus.js'; console.log(add(4, 5)); console.log(minus(4, 5)); </script> </body> </html>
必须在服务器上打开页面才能正常运行,否则fiel协议会报跨域
借用http-server在服务器上打开就会正常运行
npx http-server
-
标签:http,esModule,module,js,add,html,export,minus,加载 来源: https://www.cnblogs.com/fqh123/p/16342801.html