1.默认导出(export default)
- 一个模块只能有一个默认导出
- 导入时可以使用任意名称
- 通常用于导出模块的主要功能或值
导出
const mainFunction = () => {
console.log("hello")
};
export default mainFunction
导入
import myFunction from './module.js'
myFunction()
2.具名导出(export)
- 一个模块可以有多个具名导出
- 导入时需要与导出相同的名称
- 通常用于导出多个辅助功能或值
导出
export const functionA = () => {
console.log("This is function A")
}
export const functionB = () => {
console.log("This is function B")
}
导入
import { functionA, functionB } from './module.js'
functionA()
functionB()
使用别名
import { functionA as foo, functionB as bar } from './module.js'
foo()
bar()
3.同时使用默认导出和具名导出
导出
const mainFunction = () => {
console.log("This is the default export")
}
export const helperFunction = () => {
console.log("This is a named export")
}
export default mainFunction
导入
import mainFunction, { helperFunction } from './module.js'
mainFunction()
helperFunction()
4.总结对比
