当前位置:网站首页>Export default the exported object cannot be deconstructed, and module Differences between exports
Export default the exported object cannot be deconstructed, and module Differences between exports
2022-07-02 00:20:00 【Ronin alley 7】
Recently in the process of doing the project , There's a problem , To a UI Components , Deconstruct the components we need , To register , But there was an error in the operation , I wrote a little example
//user.js
export default {
obj:{name:1111},
obj2:{name:22222},
}
// index.js
import {obj,obj2} from "./user.js"
console.log(obj,obj2)An error is reported when the code is running and compiling

Said he didn't find obj and obj2, Why is that ?
Now let's go into webpack Take a deep look at the principle of packaging , adopt import What is imported ?
Let's modify the code
//user.js
export default {
obj:{name:1111},
obj2:{name:22222},
}
// index.js
// index.js page
import user from "./user.js"
console.log(user)adopt webpack Pack up , And let's see , After packing , stay main.js, hold index.js What does it look like to package and compile

seen webpack Everyone who packages the source code knows , adopt import Import when packaging
import Will be replaced by __webapck_require_ Method , To get , Parameters 1 Because we now have two files , One is index.js, One is user.js, When packing , Will put them in an array ,1 The code is in the array index Location
We are console Input user, But after packaging, an object is returned to get the object default attribute ,
_user_js__WEBPACK_IMPORTED_MODULE_0__ This returns an object , We knew , Through export In the exported file , Returns a similar
var _user_js__WEBPACK_IMPORTED_MODULE_0__ = {
default:...,
}import user from "./user.js"
console.log(_user_js__WEBPACK_IMPORTED_MODULE_0__["default"])
user The value taken is the return object default Property value , But not directly in import Deconstruct here , Because he hasn't converted yet , We can assign values first , Deconstruction
import user from "./user.js"
let { obj, obj2 } = user
So we can deconstruct , because user What you get is the return object default The attribute value
We can also get the large object returned
// user.js
export default {
obj:{name:1111},
obj2:{name:22222},
}
export const name = ' Kobe '
// index.js
import * as user from "./user.js"
console.log(user)Print it out and have a look , You can get all the objects

that export and module.exports What's the difference ?
module.exports The export of will be simple , Take a look webpack Of main.js file
(() => {
"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()))
})();adopt __webpack_require__ obtain news.js, Take a look __webpack_require__ The function returns

You can see __webpack_require__ Back to you module.exports, Namely news Return that array
And then it calls again __webpack_require__.n Method

Because we use module.exports, yes common.js, No __esModule, So go to the later function , So the return is still the original news.js Inside module.exports Array of
边栏推荐
- heketi 记录
- ADO. Net SqlConnection object usage summary
- js 公共库 cdn 推荐
- Learn online case practice
- 一个实习生的CnosDB之旅
- Which app is better and more secure for stock mobile account opening
- 数据库--SqlServer详解
- Jielizhi Bluetooth headset quality control and production skills [chapter]
- Is it safe for qiniu college to open an account? How to open an account?
- Node——生成微信权限验证配置
猜你喜欢

Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?

SQL数据分析之窗口排序函数rank、dense_rank、raw_number与lag、lead窗口偏移函数【用法整理】

起床困难综合症(按位贪心)

Multi table operation - one to one, one to many and many to many
![Data analysis methodology and previous experience summary [notes dry goods]](/img/00/e4c4cf37f1ca9134546f970d800226.png)
Data analysis methodology and previous experience summary [notes dry goods]

【QT】對於Qt MSVC 2017無法編譯的問題解决

PWN attack and defense world cgpwn2

Ldr6035 smart Bluetooth audio can be charged and released (5.9.12.15.20v) fast charging and fast releasing device charging

RPA tutorial 01: Excel automation from introduction to practice

mysql之B tree 以及 B+tree
随机推荐
【QT】對於Qt MSVC 2017無法編譯的問題解决
[QT] test whether QT can connect to the database
const // It is a const object... class nullptr_ t
股票开户哪个证券公司比较安全
使用htaccess文件禁止目录里的脚本执行权限
Linux CentOS7安装Oracle11g的超完美新手教程
export default 导出的对象,不能解构问题,和module.exports的区别
B tree and b+tree of MySQL
LeetCode 0241.为运算表达式设计优先级 - DFS
Review data desensitization system
Is it safe for qiniu college to open an account? How to open an account?
Vue force cleaning browser cache
I would like to ask, which securities is better for securities account opening? Is it safe to open a mobile account?
Shell process control
二叉搜索树的创建,查找,添加,删除操作
What is the purpose of ERP project implementation plan?
leetcode96不同的二叉搜索樹
Selectively inhibiting learning bias for active sampling
Jielizhi Bluetooth headset quality control and production skills [chapter]
mysql之B tree 以及 B+tree