鍍金池/ 問答/網(wǎng)絡(luò)安全  HTML/ 關(guān)于ES6module中export和import命令

關(guān)于ES6module中export和import命令

  1. 是看阮一峰的ES6中的演示代碼
  2. clipboard.png

clipboard.png



報錯如下:

clipboard.png

  1. 在網(wǎng)上看到解答

    clipboard.png

  2. 使用bable轉(zhuǎn)成es5

    clipboard.png

疑問:1. 所以使用module中的必須要用module.exports和require?2.如果是按上面的方法的話不是還是將整個模塊導(dǎo)入然后再從模塊中讀取特定屬性,可是我看阮一峰的教程中是說只加載import的方法?

回答
編輯回答
礙你眼
  1. 所以使用module中的必須要用module.exports和require?

首先你是nodejs,8以上支持import-export,但要加運行參數(shù),并且改為.mjs,詳情看官網(wǎng)。

2.如果是按上面的方法的話不是還是將整個模塊導(dǎo)入然后再從模塊中讀取特定屬性,可是我看阮一峰的教程中是說只加載import的方法?

據(jù)我所知,無論require還是import都是整個模塊導(dǎo)入,這是nodejsrequire實現(xiàn)以及ES6import規(guī)范,let {foo,bar} = ...只是解構(gòu)語法。

參考鏈接MDN import

Given an object or value named myExport which has been exported from the module my-module either implicitly (because the entire module is exported) or explicitly (using the export statement), this inserts myExport into the current scope.
import {myExport} from '/modules/my-module.js';
2018年5月23日 04:12
編輯回答
風(fēng)清揚

chrome 61 開始在瀏覽器中嘗試直接支持 ES-Next的 import-export,
我這兒有一個簡單的例子
https://shy2850.github.io/mus...

2017年6月6日 21:30
編輯回答
孤影

import行不行不知道 但是我知道export不行
你完全可以自己嘗試的 這種問題實踐就知道了

2017年7月23日 05:59
編輯回答
毀憶

你可以嘗試著在babel環(huán)境下運行,當(dāng)前的瀏覽器應(yīng)該是還沒有直接支持import、export語法的。

2017年4月30日 00:42