es6模块化
- 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 m from './test/js'; // m 是可自定义名称
④ 动态import(懒加载,在需要的时候再去导入模块)
<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()); }