当前位置:网站首页>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
边栏推荐
- Accelerator systems initiative is an independent non-profit organization
- SQL数据分析之窗口排序函数rank、dense_rank、raw_number与lag、lead窗口偏移函数【用法整理】
- MySQL: the difference between insert ignore, insert and replace
- Use pair to do unordered_ Key value of map
- [QT] qtcreator uninstall and installation (abnormal state)
- Kyushu cloud and Intel jointly released the smart campus private cloud framework, enabling new infrastructure for education
- mysql之B tree 以及 B+tree
- [opencv450] hog+svm and hog+cascade for pedestrian detection
- Relatively easy to understand PID understanding
- LDR6035智能蓝牙音响可对手机设备持续充放电方案
猜你喜欢

使用多线程Callable查询oracle数据库

Leetcode 96 différents arbres de recherche binaires

Use pair to do unordered_ Key value of map
Linux centos7 installation Oracle11g super perfect novice tutorial

The new version of graphic network PDF will be released soon

SQL Server 安装指南

Correlation - intra group correlation coefficient

比较通俗易懂的PID理解

多表操作-一对一,一对多与多对多

It's nothing to be utilitarian!
随机推荐
Leetcode96 different binary search trees
数据分析方法论与前人经验总结【笔记干货】
What is ThreadLocal memory leak and how to solve it
It's nothing to be utilitarian!
Operate database transactions with jpatractionmanager
【QT】對於Qt MSVC 2017無法編譯的問題解决
const // It is a const object... class nullptr_ t
Mysql database driver (JDBC Driver) jar package download
Guide d'installation du serveur SQL
Linux CentOS7安装Oracle11g的超完美新手教程
The difference between timer and scheduledthreadpoolexecutor
USB-IF协会与各种接口的由来
【opencv】train&test HOG+SVM
比较通俗易懂的PID理解
Using SqlCommand objects in code
Dongge cashes in and the boss retires?
[CTF] bjdctf 2020 Bar _ Bacystack2
Talents come from afar, and Wangcheng district has consolidated the intellectual base of "strengthening the provincial capital"
起床困难综合症(按位贪心)
Window sorting functions rank and deny for SQL data analysis_ rank、raw_ Number and lag, lead window offset function [usage sorting]