ES6模块化主要有两个操作:import 和 export
如果在html文件的script中引用模块的话,要设置<script type="module">
一种导入导出方法:
javascript
a.js
//分别暴露
export let num = 1
export function compute(a, b){
return a + b
}
//统一暴露
export {num, compute}
javascript
//解构赋值
//import {num as num1, compute} from './a.js' 用as可以解决命名冲突的问题
import {num, compute} from './a.js'
console.log(num)
console.log(compute(1,2))
另一种导入导出方法:
javascript
a.js
export default{
num: 1,
compute: (a, b) => a + b
}
javascript
import a from './a.js'
console.log(a)
console.log(a.num)
console.log(a.compute(1, 2))