When should I use curly braces for ES6 import?

This is a default import:

// B.js
import A from './A'

It only works if A has the default export:

// A.js
export default 42

In this case it doesn't matter what name you assign to it when importing:

// B.js
import A from './A
import  MyA from './A'
import Something from '.A'

Because it will always resolve to whatever is the default export of A.
This is a named import called A:

import { A } from '.A'

It only works if A contains a named export called A:

export const A = 42 

In this case the name matters because you're importing a specific thing by its export name:

// A.js
import { A } from './A'
import { myA } from './A'     // Does't work!
import { Something } from './A'    // Does't work!

To make these work, you would add a corresponding named export to A:

// A.js
export const A =42
export const myA = 43
export const Something = 44

A module can only have one default export, but as many named exports as you'd like (zero,one,two,or many).You can import them all together:

// B.js
import A { myA, Something } from './A'

Here, we import the default export as A, and named exports called myA and Something, respectively.

// A.js
export default 42
export const myA =43
export const Something = 44

We can also assign them all different names when import:

// B.js
import X, { myA as myX, Something as XSomething } from './A'

The default exports tend to be used for whatever you normally expect to get from the module. The named exports tend to be used for utilities that might be handy, but are't always necessay. However it is up to you to choose how to export things: for example, a module might have no default at all.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市拌喉,隨后出現(xiàn)的幾起案子缸榛,更是在濱河造成了極大的恐慌,老刑警劉巖链沼,帶你破解...
    沈念sama閱讀 216,843評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異沛鸵,居然都是意外死亡括勺,警方通過查閱死者的電腦和手機缆八,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,538評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來疾捍,“玉大人奈辰,你說我怎么就攤上這事÷叶梗” “怎么了奖恰?”我有些...
    開封第一講書人閱讀 163,187評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長宛裕。 經(jīng)常有香客問我瑟啃,道長,這世上最難降的妖魔是什么揩尸? 我笑而不...
    開封第一講書人閱讀 58,264評論 1 292
  • 正文 為了忘掉前任蛹屿,我火速辦了婚禮,結(jié)果婚禮上疲酌,老公的妹妹穿的比我還像新娘蜡峰。我一直安慰自己,他們只是感情好朗恳,可當(dāng)我...
    茶點故事閱讀 67,289評論 6 390
  • 文/花漫 我一把揭開白布湿颅。 她就那樣靜靜地躺著,像睡著了一般粥诫。 火紅的嫁衣襯著肌膚如雪油航。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,231評論 1 299
  • 那天怀浆,我揣著相機與錄音谊囚,去河邊找鬼。 笑死执赡,一個胖子當(dāng)著我的面吹牛镰踏,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播沙合,決...
    沈念sama閱讀 40,116評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼奠伪,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了首懈?” 一聲冷哼從身側(cè)響起绊率,我...
    開封第一講書人閱讀 38,945評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎究履,沒想到半個月后滤否,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,367評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡最仑,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,581評論 2 333
  • 正文 我和宋清朗相戀三年藐俺,在試婚紗的時候發(fā)現(xiàn)自己被綠了炊甲。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,754評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡欲芹,死狀恐怖蜜葱,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情耀石,我是刑警寧澤,帶...
    沈念sama閱讀 35,458評論 5 344
  • 正文 年R本政府宣布爸黄,位于F島的核電站滞伟,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏炕贵。R本人自食惡果不足惜梆奈,卻給世界環(huán)境...
    茶點故事閱讀 41,068評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望称开。 院中可真熱鬧亩钟,春花似錦辛掠、人聲如沸悄谐。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,692評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽墙牌。三九已至很澄,卻和暖如春乳讥,著一層夾襖步出監(jiān)牢的瞬間聪舒,已是汗流浹背昆雀。 一陣腳步聲響...
    開封第一講書人閱讀 32,842評論 1 269
  • 我被黑心中介騙來泰國打工辱志, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人狞膘。 一個月前我還...
    沈念sama閱讀 47,797評論 2 369
  • 正文 我出身青樓揩懒,卻偏偏與公主長得像,于是被迫代替她去往敵國和親挽封。 傳聞我的和親對象是個殘疾皇子已球,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,654評論 2 354

推薦閱讀更多精彩內(nèi)容