ES6的模塊化中,export與export default都可以用於導出常量、函數、文件、模塊等,我們可以通過在其它文件或模塊中import(常量、函數、文件、模塊)的
方式導入,但在一個文件或模塊中,export、import可以有多個,export default僅有一個。
具體使用場景:
1、export方式導出
//test1.js export const baseUrl = 'http://127.0.0.1' export function fn1 () { //do something }
import導入使用
//test2.js import { fn1, baseUrl } from './test1.js' function fn2 () { //do something
ajax(baseUrl,data)
.then(res=> fn1(res))
.catch(err=> err) }
2、export default方式導出
//test1.js export default function fn1 () { //do something }
import導入使用
//test2.js import fn1 from './test1.js' function fn2 () { //do something fn1() }