一、導入和導出
如果想要在一個Js文件中用另一個js文件的代碼
1、將js文件中的變量和函數導出
let a = "aaaa" function show() { console.log("my name is show"); } export default{ a, show }
2、在另一個js文件中將其導入
import exportimport from "./exportimport"; exportimport.show(); console.log(exportimport.a);
3、執行:npx babel-node .\babelindex.js
二、按需導入和導出
如果我們只想導出方法show,而不想導出變量,修改要導入的文件
let a = "aaaa" export function show() { console.log("my name is show111"); }
在要使用導入的文件中修改,並使用show方法:
import {show} from "./exportimport"; show();