当前位置:网站首页>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
边栏推荐
- How to improve data quality
- Guide d'installation du serveur SQL
- Data analysis methodology and previous experience summary [notes dry goods]
- Multi table operation - one to one, one to many and many to many
- Selectively inhibiting learning bias for active sampling
- Window sorting functions rank and deny for SQL data analysis_ rank、raw_ Number and lag, lead window offset function [usage sorting]
- Dongge cashes in and the boss retires?
- cookie、session、tooken
- [CTF] bjdctf 2020 Bar _ Bacystack2
- Is it safe for qiniu college to open an account? How to open an account?
猜你喜欢
数据分析方法论与前人经验总结【笔记干货】
leetcode96不同的二叉搜索树
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
Mysql database driver (JDBC Driver) jar package download
Use pair to do unordered_ Key value of map
Database -- sqlserver details
毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
Window sorting functions rank and deny for SQL data analysis_ rank、raw_ Number and lag, lead window offset function [usage sorting]
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
Selectively inhibiting learning bias for active sampling
随机推荐
下载在线视频 m3u8使用教程
leetcode96不同的二叉搜索樹
PHP reads ini or env type configuration
Node——添加压缩文件
LeetCode中等题题分享(5)
13 MySQL constraint
[embedded system course design] a single key controls the LED light
在证券账户上买基金安全吗?哪里可以买基金
Heketi record
Ldr6035 smart Bluetooth audio can be charged and released (5.9.12.15.20v) fast charging and fast releasing device charging
SQL Server Installation Guide
Guide d'installation du serveur SQL
【模板】自适应辛普森积分
LDR6035智能蓝牙音响可对手机设备持续充放电方案
JS——图片转base码 、base转File对象
【mysql 07】GPG key retrieval failed: “Couldn‘t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022“
Flow control statement of SQL data analysis [if, case... When detailed]
From 20s to 500ms, I used these three methods
使用htaccess文件禁止目录里的脚本执行权限
Leetcode 96 différents arbres de recherche binaires