ECMAScript(十三)export和import
Export和import模块
Export命令用于规定模块的对外接口
import命令用于输入其他模块提供的功能
//test1.js
export let a = '你好呀';
let b = '我很好';
export function add() {
console.log('我是加法');
}
function study() {
console.log('我在学习');
}
export { b, study };
//test.js
import { a, b, add, study as stu } from './test1.js';
console.log(a);
console.log(b);
add();
stu();
如果出现以下错误
出现“SyntaxError: Cannot use import statement outside a module”
1、 npm init -y
2、 在 package.json 中添加字段 type
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "test.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}

浙公网安备 33010602011771号