当前位置:网站首页>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
边栏推荐
- Example explanation: move graph explorer to jupyterlab
- 九州云与英特尔联合发布智慧校园私有云框架,赋能教育新基建
- Is it safe to buy funds in a securities account? Where can I buy funds
- 时间复杂度与空间复杂度
- GCC compilation
- Download the online video m3u8 tutorial
- 二叉搜索树的创建,查找,添加,删除操作
- 启牛商学院给的证券账户安不安全?哪里可以开户
- Guide d'installation du serveur SQL
- Leetcode 96 différents arbres de recherche binaires
猜你喜欢

Kyushu cloud and Intel jointly released the smart campus private cloud framework, enabling new infrastructure for education

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

The new version of graphic network PDF will be released soon

SQL数据分析之子查询的综合用法和案例题【耐心整理】

leetcode96不同的二叉搜索樹

Digital transformation has a long way to go, so how to take the key first step
Linux CentOS7安装Oracle11g的超完美新手教程

USB-IF协会与各种接口的由来

JS——图片转base码 、base转File对象

Learn online case practice
随机推荐
Review data desensitization system
What is the purpose of ERP project implementation plan?
【opencv】train&test HOG+SVM
E-commerce RPA robot helps brand e-commerce to achieve high traffic
ERP项目施行计划的目的是什么?
【CMake】Qt creator 里面的 cmake 配置
[QT] test whether QT can connect to the database
Operate database transactions with jpatractionmanager
2022拼多多详情/拼多多商品详情/拼多多sku详情
JS——图片转base码 、base转File对象
【CTF】bjdctf_2020_babystack2
Relevant settings of wechat applet cache expiration time (recommended)
Shell process control
Regular expression collection
SQL Server Installation Guide
实例讲解将Graph Explorer搬上JupyterLab
Node——添加压缩文件
Asp . Text of automatic reply to NETCORE wechat subscription number
An intern's journey to cnosdb
Leetcode medium question sharing (5)