module, 顾名思义就是模块化。

一开始都是社区, 前端圈的大佬们自己实现模块化, 比如seajs, nodejs使用的CommonJS规范, 以及异步模块定义 AMD,官方的ES6 Module, 同时ES6 Module也是我们目前前端工程化开发中运用最广泛的模块化规范

具体有关于前端模块化可参照下面这篇文章了​​​​​​​

聊一聊前端模块化_ikkkp_的博客-CSDN博客

在script标签中写js代码,或者使用src引入js文件时,默认不能使用module形式,即不能使用import导入文件,但是我们可以再script标签上加上type=module属性来改变方式。

//module.js
export default function test(){
  return 'test'
}
// index.js
import test from './module.js';
console.log(test())

下面通过两种方法展示js模块的引用: 

// index.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>
  // 方法 1 : 引入module.js,然后在script标签里面调用
  <script type="module">
    import test from './module.js';
    console.log(test())
  </script>
 
  // 方法 2 : 直接引入index.js,使用src引入
  <script type="module" src="./index.js"></script>
 
</body>
</html>

使用方法1:要从模块导入多个名称,或者从在一个<script>标签引入多个模块

使用方法1:想要导入多个模块,就得需要多个<script>标签

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐