当前位置:网站首页>export default 导出的对象,不能解构问题,和module.exports的区别
export default 导出的对象,不能解构问题,和module.exports的区别
2022-07-02 00:15:00 【浪人巷子7】
最近在做项目过程中,遇到一个问题,对一个UI组件,解构出我们需要的组件,进行注册,但是运行的时候报错了,写了一个小例子
//user.js
export default {
obj:{name:1111},
obj2:{name:22222},
}
// index.js
import {obj,obj2} from "./user.js"
console.log(obj,obj2)代码运行编译时候报错了

说没有找到obj和obj2,这是为什么?
下面我们深入webpack打包的原理深入去看下,通过import导入进来的是一个什么?
我们来把代码修改下
//user.js
export default {
obj:{name:1111},
obj2:{name:22222},
}
// index.js
// index.js 页面
import user from "./user.js"
console.log(user)通过 webpack打包下,然后来看下,打包完以后,在main.js,把index.js打包编译成什么样子

看过webpack打包源码的都知道,通过import导入在打包的时候
import会被替换成__webapck_require_方法,去获取,参数1是因为我们现在有两个文件,一个是index.js,一个是user.js,在打包的时候,会把他们放在一个数组里面,1代码是在数组的index位置
我们在console里面输入的user,但是打包后是返回一个对象取对象里面的default属性,
_user_js__WEBPACK_IMPORTED_MODULE_0__这返回的是一个对象,我们就知道,在通过export导出的文件里面,返回的是一个类似
var _user_js__WEBPACK_IMPORTED_MODULE_0__ = {
default:...,
}import user from "./user.js"
console.log(_user_js__WEBPACK_IMPORTED_MODULE_0__["default"])
user取的值是返回对象的default属性值,但是不可以直接在import这里解构,因为他还没有进行转换,我们可以先赋值,再解构
import user from "./user.js"
let { obj, obj2 } = user
这样我们是可以解构,因为user拿到的是返回对象的 default 的属性值
我们也可以拿到返回的那个大对象
// user.js
export default {
obj:{name:1111},
obj2:{name:22222},
}
export const name = '科比'
// index.js
import * as user from "./user.js"
console.log(user)打印出来看下,可以拿到所有的对象

那 export 和 module.exports 有什么区别呢?
module.exports的导出会简单,来看下webpack的main.js文件
(() => {
"use strict";
__webpack_require__.r(__webpack_exports__);
var _news_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
var _news_js__WEBPACK_IMPORTED_MODULE_0___default = __webpack_require__.n(_news_js__WEBPACK_IMPORTED_MODULE_0__);
console.log((_news_js__WEBPACK_IMPORTED_MODULE_0___default()))
})();通过__webpack_require__获取 news.js,来看下__webpack_require__函数返回的内容

可以看到 __webpack_require__返回的就是module.exports,就是news返回那个数组
然后又调用了__webpack_require__.n方法

因为我们用的是module.exports,是common.js,不是__esModule,所以走后面那个函数,所以返回的还是原来的news.js里面 module.exports的数组
边栏推荐
- Ldr6035 smart Bluetooth audio can continuously charge and discharge mobile devices
- Material design component - use bottomsheet to show extended content (I)
- Example explanation: move graph explorer to jupyterlab
- Kubernetes resource object introduction and common commands (III)
- vue 强制清理浏览器缓存
- [QT] solve the problem that QT MSVC 2017 cannot compile
- ERP项目施行计划的目的是什么?
- leetcode96不同的二叉搜索树
- Resumption of attack and defense drill
- Key points of security agreement
猜你喜欢

数据分析方法论与前人经验总结【笔记干货】

ThreadLocal内存泄漏是什么,怎么解决

Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
![Jielizhi, production line assembly link [chapter]](/img/f8/20c41ffe9468d59bf25ea49f73751e.png)
Jielizhi, production line assembly link [chapter]

How to solve the image pop-up problem when pycharm calls Matplotlib to draw

Dongge cashes in and the boss retires?

电商RPA机器人,助力品牌电商抢立流量高点
![[QT] test whether QT can connect to the database](/img/63/32530c15995ef23bde8cadc3adfd11.png)
[QT] test whether QT can connect to the database

PyCharm调用matplotlib绘图时图像弹出问题怎么解决

Algolia's search needs are almost closed
随机推荐
数据分析方法论与前人经验总结【笔记干货】
LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电
Deep learning | three concepts: epoch, batch, iteration
I would like to ask, which securities is better for securities account opening? Is it safe to open a mobile account?
E-commerce RPA robot helps brand e-commerce to achieve high traffic
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
[es practice] safe operation mode on ES
mysql之B tree 以及 B+tree
LDR6035智能蓝牙音响可对手机设备持续充放电方案
Relatively easy to understand PID understanding
Record the accidental success and failure of uploading large files
Jielizhi Bluetooth headset quality control and production skills [chapter]
【CMake】Qt creator 里面的 cmake 配置
[QT] qtcreator uninstall and installation (abnormal state)
The difference between timer and scheduledthreadpoolexecutor
cookie、session、tooken
Is it safe to choose mobile phone for stock trading account opening in Beijing?
Is it safe for qiniu college to open an account? How to open an account?
What is the purpose of ERP project implementation plan?
比较通俗易懂的PID理解