JavaScript ES6中export及export default的區別

來源:互聯網
上載者:User

標籤:world   str   連結   name   detail   targe   .net   class   number   

本文原創地址連結:http://blog.csdn.net/zhou_xiao_cheng/article/details/52759632,未經博主允許不得轉載。
相信很多人都使用過export、export default、import,然而它們到底有什麼區別呢?
在JavaScript ES6中,export與export default均可用於匯出常量、函數、檔案、模組等,

你可以在其它檔案或模組中通過import+(常量 | 函數 | 檔案 | 模組)名的方式,將其匯入,以便能夠對其進行使用,但在一個檔案或模組中,export、import可以有多個,export default僅有一個。
具體使用:
1、

//demo1.jsexport const str = ‘hello world‘export function f(a){ return a+1}
  • 1
  • 2
  • 3
  • 4

對應的匯入方式:

//demo2.jsimport { str, f } from ‘demo1‘ //也可以分開寫兩次,匯入的時候帶花括弧

2、

//demo1.jsexport default const str = ‘hello world‘
  • 1

對應的匯入方式:

//demo2.jsimport str from ‘demo1‘ //匯入的時候沒有花括弧

JavaScript ES6中export及export default的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.