当前位置:网站首页>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的数组
边栏推荐
- How to realize parallel replication in MySQL replication
- Asp . Text of automatic reply to NETCORE wechat subscription number
- cookie、session、tooken
- Soft exam information system project manager_ Compiled abbreviations of the top ten management processes to help memory recitation - -- software test advanced information system project manager 054
- Asp .NetCore 微信订阅号自动回复之文本篇
- Jielizhi Bluetooth headset quality control and production skills [chapter]
- Pytorch learning record
- The difference between timer and scheduledthreadpoolexecutor
- Jielizhi, production line assembly link [chapter]
- LeetCode中等题题分享(5)
猜你喜欢
Various global files related to [.Net core] program
比较通俗易懂的PID理解
Algolia's search needs are almost closed
Dongge cashes in and the boss retires?
SQL数据分析之流程控制语句【if,case...when详解】
E-commerce RPA robot helps brand e-commerce to achieve high traffic
Leetcode96 different binary search trees
Heketi record
Qt5.12.9 migration tutorial based on Quanzhi H3
LeetCode中等题题分享(5)
随机推荐
SecurityUtils.getSubject().getPrincipal()为null的问题怎么解决
Using multithreaded callable to query Oracle Database
【QT】Qt 使用MSVC2017找不到编译器的解决办法
ADO. Net SqlCommand object
Windows installation WSL (II)
Mysql database driver (JDBC Driver) jar package download
数据分析方法论与前人经验总结【笔记干货】
Record the accidental success and failure of uploading large files
Ldr6035 smart Bluetooth audio can continuously charge and discharge mobile devices
Deep learning | three concepts: epoch, batch, iteration
The origin of usb-if Association and various interfaces
Practical calculation of the whole process of operational amplifier hysteresis comparator
下载在线视频 m3u8使用教程
Is it safe to choose mobile phone for stock trading account opening in Beijing?
智能运维实战:银行业务流程及单笔交易追踪
MySQL: the difference between insert ignore, insert and replace
GCC compilation
Database -- sqlserver details
cookie、session、tooken
Guide d'installation du serveur SQL