ES6 exports與import導(dǎo)出模塊使用基礎(chǔ)示例
引言
在創(chuàng)建JavaScript模塊時,export
用于從模塊中導(dǎo)出實(shí)時綁定的函數(shù)、對象或原始值,以便其他程序可以通過 import
使用它們。
被導(dǎo)出的綁定值依然可以在本地進(jìn)行修改。
在使用import 進(jìn)行導(dǎo)入時,這些綁定值只能被導(dǎo)入模塊所讀取,但在 export 導(dǎo)出模塊中對這些綁定值進(jìn)行修改,所修改的值也會實(shí)時地更新。
exports
ES6模塊只支持靜態(tài)導(dǎo)出,只可以在模塊的最外層作用域使用export
,不可在條件語句與函數(shù)作用域中使用。
Named exports (命名導(dǎo)出)
這種方式主要用于導(dǎo)出多個函數(shù)或者變量, 明確知道導(dǎo)出的變量名稱。
使用:只需要在變量或函數(shù)前面加 export
關(guān)鍵字即可。
使用場景:比如 utils、tools、common 之類的工具類函數(shù)集,或者全站統(tǒng)一變量等。
export 后面不可以是表達(dá)式,因?yàn)楸磉_(dá)式只有值,沒有名字。
每個模塊包含任意數(shù)量的導(dǎo)出。
// lib.js export const sqrt = Math.sqrt; export function square(x) { return x * x; } export function diag(x, y) { return sqrt(square(x) + square(y)); } // index.js 使用方式1 import { square, diag } from 'lib'; console.log(square(11)); // 121 // index.js 使用方式2 import * as lib from 'lib'; console.log(lib.square(11)); // 121
簡寫格式,統(tǒng)一列出需要輸出的變量,例如上面的lib.js可以改寫成:
// lib.js const sqrt = Math.sqrt; function square(x) { return x * x; } function add (x, y) { return x + y; } export { sqrt, square, add };
Default exports (默認(rèn)導(dǎo)出)
這種方式主要用于導(dǎo)出類文件或一個功能比較單一的函數(shù)文件;
使用:只需要在變量或函數(shù)前面加 export default
關(guān)鍵字即可。
每個模塊最多只能有一個默認(rèn)導(dǎo)出;
默認(rèn)導(dǎo)出可以視為名字是default
的模塊輸出變量;
默認(rèn)導(dǎo)出后面可以是表達(dá)式,因?yàn)樗恍枰怠?/p>
導(dǎo)出一個值:
export default 123;
導(dǎo)出一個函數(shù):
// myFunc.js export default function () { ... }; // index.js import myFunc from 'myFunc'; myFunc();
導(dǎo)出一個類:
// MyClass.js class MyClass{ constructor() {} } export default MyClass; // 或者 export { MyClass as default, … }; // index.js import MyClass from 'MyClass';
export default 與 export 的區(qū)別:
- 不需要知道導(dǎo)出的具體變量名;
- 導(dǎo)入【import】時不需要 { } 包裹;
Combinations exports (混合導(dǎo)出)
混合導(dǎo)出是 Named exports
和 Default exports
組合導(dǎo)出。
混合導(dǎo)出后,默認(rèn)導(dǎo)入一定放在命名導(dǎo)入前面;
// lib.js export const myValue = ''; export const MY_CONST = ''; export function myFunc() { ... } export function* myGeneratorFunc() { ... } export default class MyClass { ... } // index.js import MyClass, { myValue, myFunc } from 'lib';
Re-exporting (別名導(dǎo)出)
一般情況下,export 導(dǎo)出的變量名是原文件中的變量名,但也可以用 as 關(guān)鍵字來指定別名。這樣做是為了簡化或者語義化 export 的函數(shù)名。
同一個變量允許使用不同名字輸出多次
// lib.js function getName() { ... }; function setName() { ... }; export { getName as get, getName as getUserName, setName as set }
Module Redirects (中轉(zhuǎn)模塊導(dǎo)出)
為了方便使用模塊導(dǎo)入,在一個父模塊中“導(dǎo)入-導(dǎo)出”不同模塊。簡單來說:創(chuàng)建單個模塊,集中多個模塊的多個導(dǎo)出。
使用:使用 export from
語法實(shí)現(xiàn);
export * from 'lib'; // 沒有設(shè)置 export default export * as myFunc2 from 'myFunc'; // 【ES2021】沒有設(shè)置 export default
export { default as function1, function2 } from 'bar.js';
上述例子聯(lián)合使用導(dǎo)入和導(dǎo)出:
import { default as function1, function2 } from 'bar.js'; export { function1, function2 };
盡管此時 export 與 import 等效,但以下語法在語法上無效:
import DefaultExport from 'bar.js'; // 有效的 export DefaultExport from 'bar.js'; // 無效的
正確的做法是重命名這個導(dǎo)出:
export { default as DefaultExport } from 'bar.js';
Importing
// Named imports import { foo, bar as b } from './some-module.mjs'; // Namespace import import * as someModule from './some-module.mjs'; // Default import import someModule from './some-module.mjs'; // Combinations: import someModule, * as someModule from './some-module.mjs'; import someModule, { foo, bar as b } from './some-module.mjs'; // Empty import (for modules with side effects) import './some-module.mjs';
以上就是ES6 exports與import導(dǎo)出模塊使用基礎(chǔ)示例的詳細(xì)內(nèi)容,更多關(guān)于ES6 exports import使用的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
JavaScript中關(guān)于Object.create()的用法
這篇文章主要介紹了JavaScript中關(guān)于Object.create()的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02微信小程序?qū)崿F(xiàn)類似微信點(diǎn)擊語音播放效果
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)類似微信點(diǎn)擊語音播放效果,不會互相干擾播放狀態(tài),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07Javascript實(shí)現(xiàn)禁止輸入中文或英文的例子
這篇文章主要介紹了Javascript實(shí)現(xiàn)禁止輸入中文或英文的方法實(shí)例,本文方法都是使用正則表達(dá)式實(shí)現(xiàn),需要的朋友可以參考下2014-12-12JS按位非(~)運(yùn)算符與~~運(yùn)算符的理解分析
按位非運(yùn)算符,簡單的理解就是改變運(yùn)算數(shù)的符號并減去1,當(dāng)然,這是只是簡單的理解能轉(zhuǎn)換成number類型的數(shù)據(jù)2011-07-07easyui-edatagrid.js實(shí)現(xiàn)回車鍵結(jié)束編輯功能的實(shí)例
下面小編就為大家?guī)硪黄猠asyui-edatagrid.js實(shí)現(xiàn)回車鍵結(jié)束編輯功能的實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04js函數(shù)setTimeout延遲執(zhí)行的簡單介紹
設(shè)置指定的JS函數(shù)在指定的時間后執(zhí)行,可以利用setTimeout()函數(shù)。2013-07-07Web版彷 Visual Studio 2003 顏色選擇器
Web版彷 Visual Studio 2003 顏色選擇器...2007-01-01