node系列--【ES6的模块化语法】

ES6模块化语法

​ export(导出、规定模块对外的接口)

​ import( 导入,用于输入外部模块提供的变量或函数)

具名导出和导入

let a = "ok";
let fn = function(){
  console.log("hello world");
}
// 1)具名导出,导出时的名称必须和声明时的名称一致。
export {
  a,
  fn
}

导入,注意script标签要加 type="module"

<script type="module">
// 1)具名导出时,导入的写法。注意导入时的名字和导出的名字必须一致。
import {a,fn} from "./01-test.js";
console.log( a );
fn()
</script>

不具名导出和导入

// 2)不具名导出:
export default {
  a,
  fn
}
//2) 不具名导出时导入的写法。注意导入后的变量和函数包含在对象中
import test from "./01-test.js";
console.log( test );
原文地址:https://www.cnblogs.com/chenhaiyun/p/14706080.html