当前位置:网站首页>commonjs导入导出与ES6 Modules导入导出简单介绍及使用
commonjs导入导出与ES6 Modules导入导出简单介绍及使用
2022-07-29 08:16:00 【闲鱼_JavaScript】
介绍
- module.exports 与 exports 是 commonjs 规范中的模块暴露方法;
- require 是 commonjs 规范中模块引入的方法;
- export default 与 export 是 ES6 Module 规范的模块暴露方法;
- import xxx from ‘./xxx’ 是 ES6 Module 规范的模块引入方法
使用
- commonjs 规范:module.exports
(1)导出(src/index.js)
(2)引用(src/test.js)const obj = { name: 'zs' }; module.exports = objconst obj = require('./index.js'); console.log(obj) // { name: 'zs' } - commonjs 规范:exports
(1)导出(src/index.js)
(2)引用(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 规范:export default
(1)导出(src/index.js)
(2)引用(src/test.js)const obj = { name: 'zs' }; export default objimport test from './index.js' console.log(test) // { name: 'zs' } - ES6 Module 规范:export
(1)导出(src/index.js)
(2)引用(src/test.js)export const obj = { name: 'zs' };
(3)导出(src/index-2.js)import { obj } from './index.js' import test from './index.js' console.log(obj) // { name: 'zs' } console.log(test) // 提示你没有使用 export default;
(4)引用(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'}
注意事项
commonjs 规范
- 同时使用 module.exports 暴露与 exports 暴露;最终读取到的结果是 module.exports 暴露的数据;与暴露的顺序无关;
// 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 } - 由上得出结论:modules.exports 与 exports 不能同时使用
ES Module 规范
- 如果使用 export 暴露引用为 import test from ‘./test.js’;则找不到模块,需 export default;
错误信息:"export 'default' (imported as 'test') was not found in './test.js' - 如果使用 export default 暴露引用为 import { obj } from ‘./test.js’;则找不到模块,需 export;
错误信息:"export 'age' was not found in './test.js' - export default 和 export 可以同时使用;以下为试验代码;
(1)导出(src/index-2.js)
(2)引用(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'}
边栏推荐
- Unity beginner 4 - frame animation and protagonist attack (2D)
- Compatible with cc1101/cmt2300-dp4301 sub-1g wireless transceiver chip
- Unity beginner 3 - enemy movement control and setting of blood loss area (2D)
- 简易计算器微信小程序项目源码
- Solve the problem of MSVC2017 compiler with yellow exclamation mark in kits component of QT
- BiSeNet v2
- Qt/pyqt window type and window flag
- Ue4/ue5 C disk enlargement processing
- Proteus simulation based on msp430f2491 (realize water lamp)
- Alibaba political commissar system - Chapter 4: political commissars are built on companies
猜你喜欢

Alibaba political commissar system - Chapter 4: political commissars are built on companies

Arduinoide + stm32link burning debugging

Huawei wireless device configuration uses WDS technology to deploy WLAN services

深度学习(2):图片文字识别
![[beauty of software engineering - column notes] 28 | what is the core competitiveness of software engineers? (next)](/img/c1/49b4610c483933442fb96b8e3f5315.png)
[beauty of software engineering - column notes] 28 | what is the core competitiveness of software engineers? (next)

STM32 MDK (keil5) contents mismatch error summary

(视频+图文)机器学习入门系列-第5章 机器学习实践

Time function in MySQL

Qt/pyqt window type and window flag

Windows 安装 MySQL 5.7详细步骤
随机推荐
Application scheme of charging pile
Explanation and closing method of server 135, 137, 138, 139, 445 and other ports
Exercise: store department information
110 MySQL interview questions and answers (continuously updated)
Internet worm
Cyberpunk special effect shader
Unity multiplayer online framework mirror learning record (I)
MySQL中的时间函数
Ws2812b color lamp driver based on f407zgt6
产品推广的渠道和策略,化妆品品牌推广方法及步骤
Time function in MySQL
DC motor control system based on DAC0832
C language interview preparation I (about understanding Department)
pnpm install出现:ERR_PNPM_PEER_DEP_ISSUES Unmet peer dependencies
[beauty of software engineering - column notes] "one question and one answer" issue 2 | 30 common software development problem-solving strategies
C language problems
STM32 MDK (keil5) contents mismatch error summary
Character shader exercise
Redshift 2.6.41 for maya2018 watermark removal
Stm8s003 domestic substitute for dp32g003 32-bit microcontroller chip