当前位置:网站首页>es6模块化
es6模块化
2022-07-04 13:12:00 【梦幻之光_dream】
1.node.js中如何实现模块化规范
node.js遵循了CommonJS的模块化规范。
导入其他模块使用require()方法。
模块对外共享成module.exports对象。
2.模块化的好处
大家都遵循同样的模块化规范写代码,降低了沟通的成本,极大方便了各个模块之间的相互调用。
3.前端模块化规范的分类
在es6模块化诞生之前,JavaScript社区是用AMD、CMD、CommonJS等模块化规范。
AMD、CMD适用于浏览器端。
CommonJS适用于服务器端。
大多数模块化规范给开发者增加了学习的难度和开发的成本。因此,es6模块规范诞生了。
4.es6模块化规范:它是浏览器端与服务器端通用的模块化开发标准。
es6模块化规范中定义:
每个js文件都是一个独立的模块。
导入其他模块成员使用import关键字。
向外共享模块成员使用export关键字。
在node.js中支持CommonJS模块化规范,若想基于node.js学习es6模块化的语法,可以按照如下两个步骤进行配置:
在package.json的根节点中添加”type”:”module”节点。
快速初始化一个包管理配置文件-package.json的命令:
npm init -y
命令可以在代码终端进行,如果有问题,可以配置环境变量。
5.es6模块化的基本语法:有3种语法。
默认导出与默认导入
默认导出语法:export default { 默认导出的成员(变量或函数) }
注意事项:在每个模块中使用唯一的一次export default,否则会报错。
默认导入语法:import 接受名称 from ‘模块标识符’
例子:import m1 from ./out.js
注意事项:接受名称只要合法就行。
注意事项:终端写命令的时候,如果出现module找不到的问题,那么就要看路径有没有问题。
cd 路径地址 → 可以切换路径
cls → 清除
按需导出与按需导入
按需导出语法:export 按需导出的成员
按需导入语法:import { s1 } from ‘模块标识符’
注意事项:如果有多个成员变量,就用逗号隔开。
每个模块中可以使用多次按需导出。
按需导入的名称与按需导出的名称保持一致。
按需导入时,可以使用as关键字进行重命名。
按需导入可以和默认导出一起使用。
直接导入并执行模块中的代码
语法:import ‘./direct-run.js’
边栏推荐
- First experience of ViewModel
- 2022 game going to sea practical release strategy
- What is the difference between Bi financial analysis in a narrow sense and financial analysis in a broad sense?
- Programmer turns direction
- Intelligence d'affaires bi analyse financière, analyse financière au sens étroit et analyse financière au sens large sont - ils différents?
- The game goes to sea and operates globally
- Data Lake (13): spark and iceberg integrate DDL operations
- [MySQL from introduction to proficiency] [advanced chapter] (IV) MySQL permission management and control
- 商业智能BI财务分析,狭义的财务分析和广义的财务分析有何不同?
- A collection of classic papers on convolutional neural networks (deep learning classification)
猜你喜欢
软件测试之测试评估
leetcode:6109. Number of people who know the secret [definition of DP]
An overview of 2D human posture estimation
PyTorch的自动求导机制详细解析,PyTorch的核心魔法
LVGL 8.2 Line wrap, recoloring and scrolling
Data warehouse interview question preparation
失败率高达80%,企业数字化转型路上有哪些挑战?
开发中常见问题总结
Rich text editing: wangeditor tutorial
sql优化之explain
随机推荐
Leetcode 61: 旋转链表
Ultrasonic distance meter based on 51 single chip microcomputer
Compile oglpg-9th-edition source code with clion
Practical puzzle solving | how to extract irregular ROI regions in opencv
C # WPF realizes the real-time screen capture function of screen capture box
R language uses dplyr package group_ The by function and the summarize function calculate the mean and standard deviation of the target variables based on the grouped variables
[algorithm leetcode] interview question 04.03 Specific depth node linked list (Multilingual Implementation)
LVGL 8.2 Line
Map of mL: Based on Boston house price regression prediction data set, an interpretable case is realized by using the map value to the LIR linear regression model
LiveData
使用CLion编译OGLPG-9th-Edition源码
R language ggplot2 visualization: gganimate package creates animated graph (GIF) and uses anim_ The save function saves the GIF visual animation
LifeCycle
Free, easy-to-use, powerful lightweight note taking software evaluation: drafts, apple memo, flomo, keep, flowus, agenda, sidenote, workflow
Opencv3.2 and opencv2.4 installation
R language uses the DOTPLOT function of epidisplay package to visualize the frequency of data points in different intervals in the form of point graph, and uses the by parameter to specify the groupin
ML之shap:基于boston波士顿房价回归预测数据集利用shap值对XGBoost模型实现可解释性案例
Redis daily notes
Map of mL: Based on Boston house price regression prediction data set, an interpretable case of xgboost model using map value
leetcode:6109. 知道秘密的人数【dp的定义】