1. 程式人生 > >ES6 export和export default的區別

ES6 export和export default的區別

在ES6中export和export default均可用於匯出檔案、常量、模組、函式等,在匯入這些檔案時需要配合 import 使用。

在一個檔案中可以存在多個export,但只能存在一個export default:

// export匯出用法:

export function a () {

}

export const name = 'Ycz';

// export匯入用法(匯入例項需帶'{}'且例項名必須與匯出的例項名一致):

import {a, name} from 'xxxxx'

// export default匯出用法:

const obj = {

name: 'YYYYcz'

};

export default obj;

// export default匯入用法(匯入例項不需要帶'{}'且例項名也不必與匯出的例項名一致):

import defaultObj from 'xxxxx'