export和export default的区别


export的使用

1.直接输出

export let words = 'hello world!!!' 

export function output() {
  // ...
}

2.先定义再输出

let firstWords = 'hello'
let secondWords = 'world'
let thirdWords = '!!!'

function output() {
    // ...
}

export {firstWords, secondWords, thirdWords, output}

export default的使用

1.export default 用于规定模块的默认对外接口
2.很显然默认对外接口只能有一个,所以 export default 在同一个模块中只能出现一次
3.export default只能直接输出,不能先定义再输出。

4.其在 import 方式上也和 export 存在一定区别

(1)export的输出与import输入

export function output() {
    // ...
}

import {output} from './example'

(2)export default的输出与import输入

export default function output() {
    // ...
}

import output from './example'

从以上两种 import 方式即可看出,export default 的 import 方式不需要使用大括号包裹。因为对于 export default 其输出的本来就只有一个接口,提供的是模块的默认接口,自然不需要使用大括号包裹。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM