首先搞清楚在什么时候可以用什么样的语句
只有ES6支持:import、export、export default
只有node支持 : module.exports 、 exports
ES6与node都支持 : require
export 与 export defalut
export default是默认接口,在一个文件夹中只能定义一个,而export可以定义无数个。
export 导出的引入是必须要用{}接收,而export default不需要,但是也可以用{}接收
export default 引入时可以与导出时的名字不同,而export不可以。
export可直接导出变量表达式,而export default不可以
//export default 方式
const a = 10
const b = 20
const sayhello = function(){
console.log('hello');
}
//导出方式
export default {
a,
b,
sayhello
}
//引入方式
import {a,b,sayhellos} from './exp.js
import obj from './exp.js
obj.a obj.b......
//export 方式
const a = 10
const b = 20
const sayhello = function(){
console.log('hello');
}
//导出方式
export {
a,
b,
sayhello
}
export const a = 10
export const b = 20
export const sayhello = function(){
console.log('hello');
}
//引入方式
import {a,b,sayhellos} from './exp.js
import * as obj from './exp.js
obj.a obj.b......
moudle.exports 与exports
exports是moudule.exports的引用
exports不可赋对象值
module.exports = {
a,
b,
c
}
exports.a = a
exports.b = b
exports.c = c