export与export default exports与module.exports的用法
转载:http://blog.csdn.net/zhou_xiao_cheng/article/details/52759632
本文原创地址链接:http://blog.csdn.net/zhou_xiao_cheng/article/details/52759632,未经博主允许不得转载。
export与export default exports与module.exports的用法
-------------------------------------------------------
// export
export function tt(tagName, attrs, children) {
return new Element(tagName, attrs, children)
}
export const aa = '123';
// 引用
import { tt,aa } from 'aa'
-------------------------------------------------------
// export default 导出只能有一个
export default const str = 'hello world';
export default function tt {
console.log("hello world")
}
// 引用的时候就不用中括号了
import aa from 'demo1'
// 也可以不用函数名字,因为在js中,下边这两个达到同样的效果
var ss = function () {}
var ss = function ss1() {}
-------------------------------------------------------
// module.exports 同一下的exports
-------------------------------------------------------
// exports
exports.findNodeById = findNodeById;
exports.deepCopy = deepCopy
exports.treeRecursion = treeRecursion;
或者
exports = {
aa: 123,
bb:function(){}
}
// 引用
import recursion from '@/commons/js/recursion.js'
this.leafNode = recursion.treeRecursion(result,[]);
-------------------------------------------------------
如果同时在代码中导出module.exports 和exports的话。而模块导出的时候,真正导出的执行是module.exports,而不是exports
看到这里,相信大家都看到答案了,exports是引用 module.exports的值。module.exports 被改变的时候,exports不会被改变,而模块导出的时候,真正导出的执行是module.exports,而不是exports
再看看下面例子
// foo.js
exports.a = function(){
console.log('a')
}
module.exports = {a: 2}
exports.a = 1
// test.js
var x = require('./foo');
console.log(x.a)
result:
2
export与export default exports与module.exports的用法的更多相关文章
- exports与module.exports,export与export default 之间的关系和区别
首先我们要明白一个前提,CommonJS模块规范和ES6模块规范完全是两种不同的概念. CommonJS模块规范 Node应用由模块组成,采用CommonJS模块规范. 根据这个规范,每个文件就是一个 ...
- exports、module.exports 和 export、export default
先了解他们的使用范围. require: node 和 es6 都支持的引入export / import : 只有es6 支持的导出引入module.exports / exports: 只有 no ...
- NodeJS的exports、module.exports与ES6的export、export default深入详解
前言 决定开始重新规范的学习一下node编程.但是引入模块我看到用 require的方式,再联想到咱们的ES6各种export .export default. 阿西吧,头都大了.... 头大完了,那 ...
- exports与module.exports的区别,export与export.defult区别
在JS模块化编程中,之前使用的是require.js或者sea.js.随着前端工程化工具webpack的推出,使得前端js可以使用CommonJS模块标准或者使用ES6 moduel特性. 在Comm ...
- exports与module.exports的区别,以及export与export.defult的区别
在 JS 模块化编程的模块引入上, 主要有两种方式: CommonJS 模块标准 ES6 moduel 特性 1. CommonJS 模块引入:require() 模块导出:exports 或者 mo ...
- exports 和 module.exports
首先参考一个js的示例 app.js var a = {name: 'nswbmw 1'}; var b = a; console.log(a); console.log(b); b.name = ' ...
- Node.js之exports与module.exports
每一个node.js执行文件,都自动创建一个module对象,同时,module对象会创建一个叫exports的属性,初始化的值是 {} module.exports = {}; Node.js为了方 ...
- Node.js中exports,module.exports以及require方法
在Node.js中,使用module.exports.f = ...与使用exports.f = ...是一样的,此时exports就是module.exports的一种简写方式.但是,需要注意的是, ...
- exports 与 module.exports 的区别
exports与module.exports的作用就是将方法或者是变量暴露出去,以便给其他模块调用,再直接点,就是给其他模块通过require()的方式引用. 那么require()一个模块时,到底做 ...
随机推荐
- JavaScript循环练习2
折纸:折多少次和珠穆朗玛峰一样高1.一张纸的厚度是0.0001米,将纸对折,对折多少次厚度超过珠峰高度8848米 var hou = 0.0001; var cishu = 0; for(var i= ...
- SSM三层模型之间的参数传递
Controller接收各种形式的传参: RequestMapping()包含三部分信息: 表单传参: 1.表单action请求 1:参数名称和顺序都要和表单字段名称一致,直接接收(适合字 ...
- yii2redis安装
yii2 – redis 配置 转自:http://www.fancyecommerce.com/2016/05/03/yii2-redis-%E9%85%8D%E7%BD%AE/ 安装redis w ...
- 2018-2019-2 20165209 《网络对抗技术》Exp2:后门原理与实践
2018-2019-2 20165209 <网络对抗技术>Exp2:后门原理与实践 1 后门原理与实验内容 1.1 后门原理 -后门就是不经过正常认证流程而访问系统的通道. 哪里有后门呢? ...
- Linux服务器---配置apache支持php
apache支持php php是最好用的服务器语言了,Apache对php有很强大的支持 1.检测是否安装php,如果什么信息也没有,那么你就要自己安装php了 [root@localhost ~]# ...
- linux服务器管理员的12个有用的命令
ifconfig: 在修改内核中已有的网络接口时,你会用到ifconfig命令.这个命令通常用于系统调校和调试,但同时也可以用于在启动过程中设置接口. netstat: 对于Linux用户来说这是一个 ...
- 解决Android Studio Conflict with dependency 'com.android.support:support-annotations'报错
解决Android Studio Conflict with dependency 'com.android.support:support-annotations'报错 在Android Studi ...
- 可持久化fhq-treap学习笔记
目录 可持久化fhq-treap----- 支持查询历史版本的非旋treap 先看看为啥他可以可持久化 过程 别的 注意&&出错&&吐槽 模板->luoguP38 ...
- JavaScript callee caller
caller是function的属性 callee是arguments的属性 callee:返回正在执行的函数对象. var sum = function (n) { == n) ; ); } con ...
- 51nod 1202 子序列个数
1202 子序列个数 题目来源: 福州大学 OJ 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 子序列的定义:对于一个序列a=a[1],a[2] ...