当前位置:网站首页>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的数组
边栏推荐
- 数据分析方法论与前人经验总结【笔记干货】
- From 20s to 500ms, I used these three methods
- Window sorting functions rank and deny for SQL data analysis_ rank、raw_ Number and lag, lead window offset function [usage sorting]
- Linux CentOS7安装Oracle11g的超完美新手教程
- Iota in golang
- const // It is a const object... class nullptr_ t
- I would like to ask, which securities is better for securities account opening? Is it safe to open a mobile account?
- Asp .NetCore 微信订阅号自动回复之文本篇
- Use pair to do unordered_ Key value of map
- 攻防演练复盘
猜你喜欢

Windows installation WSL (II)

E-commerce RPA robot helps brand e-commerce to achieve high traffic
![[QT] QT cannot find a solution to the compiler using msvc2017](/img/80/a4b17d6cc1ab6fe1366a3a3f43ec2e.png)
[QT] QT cannot find a solution to the compiler using msvc2017

Asp . Text of automatic reply to NETCORE wechat subscription number

Openvino model performance evaluation tool DL workbench

Asp .NetCore 微信订阅号自动回复之文本篇

Algolia's search needs are almost closed

如何提升数据质量

Graduation season is both a farewell and a new beginning
![Jielizhi, production line assembly link [chapter]](/img/f8/20c41ffe9468d59bf25ea49f73751e.png)
Jielizhi, production line assembly link [chapter]
随机推荐
B tree and b+tree of MySQL
Windows10 install WSL (I) (wslregisterdistribution error)
Relevant settings of wechat applet cache expiration time (recommended)
Concurrentskiplistmap -- principle of table skipping
Ldr6035 smart Bluetooth audio can be charged and released (5.9.12.15.20v) fast charging and fast releasing device charging
【QT】Qt 使用MSVC2017找不到编译器的解决办法
微信小程序缓存过期时间的相关设置(推荐)
毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
PyCharm调用matplotlib绘图时图像弹出问题怎么解决
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
【QT】对于Qt MSVC 2017无法编译的问题解决
It's nothing to be utilitarian!
Is it safe to buy funds in a securities account? Where can I buy funds
Comprehensive usage and case questions of sub query of SQL data analysis [patient sorting]
挖财学堂开户打新债安全可靠嘛?
leetcode96不同的二叉搜索樹
求逆序数的三个方法
13 MySQL constraint
4. Object mapping Mapstercover
[QT] QT cannot find a solution to the compiler using msvc2017