/**
* 默认导出
*/
//1导出一个函数
// export default function fun() {
// console.log('XXXXXXXXXXXXX')
// }
//2导出一个对象
export default {
fun1: function () {
console.log('----')
}
}
//默认导入
import fun from './common.js'
/**
* 命名导出
*/
const fun3 = function () { }
export {
fun3
}
import { fun3 } from './common.js'
import * as fileSystem from './common.js' //导入所有模块,引用对象名为 fileSystem
import { readFile as read } from './common.js' //从 fs 导入 readFile 模块,引用对象名为 read
和require区别
1.加载方式不同
require是运行时加载,import是编译时加载;require可以写在代码的任意位置,import只能写在文件的最顶端且
require 是赋值过程。import 是解构过程。
输出
exports.hello = function () {
console.log('Hello World!');
};
导出
var obj = require('./index2.js')
obj.hello(); //输出 Hello Word!