导出1:
//小明的js
var name = '小明';
var age = 20;
function sum(num1,num2){
return num1+num2;
}
var flag = true;
if(flag){
console.log(sum(10,20));
}
//导出,只能用这个写法,flag: flag,sum: sum不行
export {
flag, sum
}
//导出函数/类
export function num(num1,num2){
return num1*num2;
}
export class People{
run(){
console.log("在奔跑!!!");
}
}
导入1:
//小明的js
//导入
import {flag} from "./aaa.js";
if(flag){
console.log("小明牛皮吖~~~");
}
//导入
import {num,People} from "./aaa.js";
console.log(num(100,1000));
const p = new People();
p.run();
导出2:
//小红的js
var obj = {};
var flag = false;
console.log("小红")
//导出方式一
export {
flag
}
//导出方式二
export var num1 = 100;
export var num2 = 200;
//只能有一个export default ,导入的时候可以随便命名,也不需要{}
const name = 'fg'
export default name;
导入2:
//小红的js
//导入
import {flag} from "./bbb.js";
if(flag){
console.log("小红好牛逼~~~");
}
import {num1,num2} from "./bbb.js";
console.log(num1);
console.log(num2);
//导出全部
import * as bbb from "./bbb.js";
console.log(bbb.num1);
console.log(bbb.num2);
//export default导入,导入的时候可以随便命名,也不需要{}
import aaa from "./bbb.js";
console.log(aaa);
html:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="./aaa.js" type="module"></script>
<script src="./bbb.js" type="module"></script>
<script src="./aaa1.js" type="module"></script>
<script src="./bbb1.js" type="module"></script>
</body>
</html>