1 | module.exports / exports: 只有 node 支持的导出 (为了避免糊涂,尽量都用 module.exports 导出) |
Node模式
1 | //utils.js |
ES6模式
export 和 export default:
1、 在一个文件或模块中,export、import可以有多个,export default仅有一个
2、 通过export方式导出,在导入时要加{ },export default则不需要
3、 export能直接导出变量表达式,export default不行。
1 | //sys-url.js |
//export.js1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17//导出变量 验证了第3条
export const a = '100';
//导出方法
export const dogSay = function(){
console.log('wang wang');
}
//导出方法第二种
function catSay(){
console.log('miao miao');
}
export { catSay };
//export default导出
const m = 100;
export default m;
//use.js1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20var router = express.Router();
//验证了第2条
import { dogSay, catSay } from './export'; //导出了 export 方法
import m from './export'; //导出了 export default
import * as testModule from './export'; //as 集合成对象导出
/* GET home page. */
router.get('/', function(req, res, next) {
dogSay();
catSay();
console.log(m);
testModule.dogSay();
console.log(testModule.m); // undefined , 因为 as 导出是 把 零散的 export 聚集在一起作为一个对象,而export default 是导出为 default属性。
console.log(testModule.default); // 100
res.send('恭喜你,成功验证');
});