学无先后,达者为师

网站首页 编程语言 正文

node支持ES6模块化练习

作者:顽强的小豆子 更新时间: 2022-07-10 编程语言

node v13.2.0之后

只需要在新建node项目(使用一个package.json文件和一个对应node_models即可)package.json中添加属性:"type": "module"

已验证好用

尝试案例

将index.html,export.js,import.js,package.json,node_modules放到一个目录下
package.json中添加属性:"type": "module"
用浏览器打开index.html即可练习es6模块化
index.html文件
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
   <!-- 看这  -->
    <script type="module" src="./import.js"></script>
  </head>
  <body></body>
</html>

export.js
export let name = 'jack';
export let age = 18;

import.js
// 名称要跟导出的模块名称一一对应,同时from后为导入模块的路径
import {name,age} from './export.js'
console.log(`我叫${name},今年${age}岁`);

原文链接:https://blog.csdn.net/qiuyushuofeng/article/details/124790029

栏目分类
最近更新