引入js文件需为 --type="module"--形式
1.对象导出{}
var name = 'cala';
var height = 1.8;
var age = 18;
var flag = true
function sum(num1, num2) {
return num1 + num2
}
if (flag) {
sum(10, 20)
}
// 1.对象导出{}
export {
name,
age,
height,
sum
}
导入为
// 1.导入对象中定义的变量
import {
name,
age,
height,
sum
} from "./export.js";
console.log(sum(10, 20));
console.log(name, age, height);
2.直接导出定义变量
export const message = 'calabash'
导入为
// 2.直接导入export定义的变量
import {message} from './export.js';
console.log(message);
3.导出函数/类
export function run(num1, num2) {
return num1 * num2
}
export class person {
zzzy() {
console.log('zy');
}
}
导入为
// 3.导入export中的function/class
import { run } from "./export.js";
console.log(run(10,20));
import { person } from "./export.js";
const z = new person();
console.log(z.zzzy);
z.zzzy()
4.export default
const add = '南京'
export default add
导入为
//4. 导入export default
import ad from './export.js'
console.log(ad);
或者导出方法
export default function (argument) {
console.log(argument);
}
导入为
import arg from './export.js'
arg(111);
5.导入所有信息
// 5.导入所有信息
import * as info from './export.js'
console.log(info.age,info.height);