当前位置:网站首页>Brief introduction and use of commonjs import and export and ES6 modules import and export
Brief introduction and use of commonjs import and export and ES6 modules import and export
2022-07-29 08:17:00 【Idle fish_ JavaScript】
Introduce
- module.exports And exports yes commonjs standard Module exposure method in ;
- require yes commonjs standard The method introduced in the module ;
- export default And export yes ES6 Module standard Module exposure method of ;
- import xxx from ‘./xxx’ yes ES6 Module standard Module introduction method
Use
- commonjs standard :module.exports
(1) export (src/index.js)
(2) quote (src/test.js)const obj = { name: 'zs' }; module.exports = obj
const obj = require('./index.js'); console.log(obj) // { name: 'zs' }
- commonjs standard :exports
(1) export (src/index.js)
(2) quote (src/test.js)exports.obj = { name: 'zs' };
const { obj } = require('./index.js'); const test = require('./index.js'); console.log(obj) // { name: 'zs' }; console.log(test) // { obj: { name: 'zs' } }
- ES6 Module standard :export default
(1) export (src/index.js)
(2) quote (src/test.js)const obj = { name: 'zs' }; export default obj
import test from './index.js' console.log(test) // { name: 'zs' }
- ES6 Module standard :export
(1) export (src/index.js)
(2) quote (src/test.js)export const obj = { name: 'zs' };
(3) export (src/index-2.js)import { obj } from './index.js' import test from './index.js' console.log(obj) // { name: 'zs' } console.log(test) // Prompt that you did not use export default;
(4) quote (src/test-2.js)export const obj = { name: 'zs' }; export default { age: 18 }
import test, { obj } from './index-2.js' console.log(test, obj) // {age: 18} {name: 'zs'}
matters needing attention
commonjs standard
- Use at the same time module.exports Exposure and exports expose ; The final read result is module.exports Exposed data ; It has nothing to do with the order of exposure ;
// src/test.js exports.arr = ['1'] module.exports = { age: 18 } exports.obj = { name: 'zs' }
// src/index.js const test = require('./test.js') console.log(test) // { age: 18 }
- Draw a conclusion from the above :modules.exports And exports Can't be used at the same time
ES Module standard
- If you use export Exposed references are import test from ‘./test.js’; Module cannot be found , Need to be export default;
error message :"export 'default' (imported as 'test') was not found in './test.js'
- If you use export default Exposed references are import { obj } from ‘./test.js’; Module cannot be found , Need to be export;
error message :"export 'age' was not found in './test.js'
- export default and export Can be used at the same time ; The following is the test code ;
(1) export (src/index-2.js)
(2) quote (src/test-2.js)export const obj = { name: 'zs' }; export default { age: 18 }
import test, { obj } from './index-2.js' console.log(test, obj) // {age: 18} {name: 'zs'}
边栏推荐
- 华为无线设备配置利用WDS技术部署WLAN业务
- BiSeNet v2
- SQL 面试碰到的一个问题
- Tle5012b+stm32f103c8t6 (bluepill) reading angle data
- Simplefoc+platformio stepping on the path of the pit
- Unity multiplayer online framework mirror learning record (I)
- Dp1332e multi protocol highly integrated contactless read-write chip
- Security baseline of network security
- 数字人民币时代隐私更安全
- Rotation in model space and rotation in world space
猜你喜欢
Arduino uno error analysis avrdude: stk500_ recv(): programmer is not responding
Privacy is more secure in the era of digital RMB
Application scheme of charging pile
Implementation of support vector machine with ml11 sklearn
Inclination sensor accuracy calibration test
DAC0832 waveform generator based on 51 single chip microcomputer
Simplefoc parameter adjustment 1-torque control
Preparation of SQL judgment statement
Redshift 2.6.41 for maya2018 watermark removal
Unicode private use areas
随机推荐
Unity beginner 2 - tile making and world interaction (2D)
Hal library learning notes - 8 concept of serial communication
A problem encountered in SQL interview
Inclination sensor accuracy calibration test
V-Ray 5 acescg workflow settings
Eps32+platform+arduino running lantern
Huawei wireless device configuration uses WDS technology to deploy WLAN services
分段分页以及段页结合
Proteus simulation based on msp430f2491 (realize water lamp)
Redshift 2.6.41 for maya2018 watermark removal
Preparation of SQL judgment statement
UE4 highlight official reference value
Cyberpunk special effect shader
pnpm install出现:ERR_PNPM_PEER_DEP_ISSUES Unmet peer dependencies
(Video + graphic) machine learning introduction series - Chapter 5 machine learning practice
Privacy is more secure in the era of digital RMB
Application of explosion-proof inclination sensor in safe operation of LNG
[beauty of software engineering - column notes] 25 | what methods can improve development efficiency?
110 MySQL interview questions and answers (continuously updated)
Solve the problem of MSVC2017 compiler with yellow exclamation mark in kits component of QT