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 其输出的本来就只有一个接口,提供的是模块的默认接口,自然不需要使用大括号包裹。

原文地址:https://www.cnblogs.com/sherrycat/p/11152994.html