- export
① 单独暴露
② 统一暴露export let a = 1; export function show() { console.log(a); }
③ 默认暴露let a = 1; function show() { console.log(a); } export { a, show }
export default { a: 1, show() { console.log('展示数据'); } }
- import
① 导入所有
② 部分导入(解构赋值方式)import * as m from './test/js';
③ 简便方式,只适用默认暴露import {a, show} from './test.js'; // 除了默认暴露都适用 import {default as m} from './test.js'; // 只适用于默认暴露
④ 动态import(懒加载,在需要的时候再去导入模块)import m from './test/js'; // m 是可自定义名称
<button id="btn">点击展示文字</button>
// show.js export function show() { console.log('hello world'); } // app.js const oBtn = document.querySelector('#btn'); oBtn.onclick = function() { // import()返回的是一个promise对象,返回值是导出的模块 import('./show.js').then(m => m.show()); }