default

named

named import 的意思是指『export 時的 variable/function 已經被命名,無法在 import 時重新取名』;如果在 import 時改名將會有 error ,確保 variable/function 保持原本的名字。

Named exports are useful to export several values. During the import, it is mandatory to use the same name of the corresponding object.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// utility.js
function parseLogs() {
// implementation
}
function generateLogs() {
// implementation
}
export { parseLogs, generateLogs };

// main.js
import { parseLogs, generateLogs } from "utility";
parseLogs();

心得

當在寫單元測試(e.g Jest)

References