We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

正在翻譯中。

import 宣告用於引入由另一個模塊所導出的綁定。被引入的模塊,無論是否宣告strict mode,都會處於該模式。import 宣告無法用於嵌入式腳本(embedded scripts)。

語法

import defaultExport from "module-name";
import * as name from "module-name";
import { export } from "module-name";
import { export as alias } from "module-name";
import { export1 , export2 } from "module-name";
import { export1 , export2 as alias2 , [...] } from "module-name";
import defaultExport, { export [ , [...] ] } from "module-name";
import defaultExport, * as name from "module-name";
import "module-name";
defaultExport
從模塊要參照過去的預設導出名。
module-name
要導入的模塊名。This is often a relative or absolute path name to the .js file containing the module. Certain bundlers may permit or require the use of the extension; check your environment. Only single quotes and double quotes Strings are allowed.
name
參照導入時,會用做 namespace 種類的模塊名。
export, exportN
導出要被引入時,要用的名號。
alias, aliasN
Names that will refer to the named imports.

敘述

name 參數能將模塊物件(module object)名用於 namespace 種類,以便各導出能參照之。export 參數會在引用 import * as name 語法時,指定 individual named export。以下示例將展示語法的簡例。

引入整個模塊

本例在當前作用域插入了 myModule 變數,並把所有來自 /modules/my-module.js 檔案的模塊導出。

import * as myModule from '/modules/my-module.js';

Here, accessing the exports means using the module name ("myModule" in this case) as a namespace. For example, if the module imported above includes an export doAllTheAmazingThings(), you would call it like this:

myModule.doAllTheAmazingThings();

從模塊引入單一導出

給定由 my-module 導出的模塊,稱作 myExport 物件與數值,無論是顯性(因為整個模塊被導出了)與隱性(使用 export 宣告),這裡就在當前的作用域插入 myExport

import {myExport} from '/modules/my-module.js';

從模塊引入數個導出

例在當前作用域插入了 foobar

import {foo, bar} from '/modules/my-module.js';

使用便利的 alias 引入或導出

在引入時,可以重新命名導出的模塊。例如說,這裡就就在目前作用域插入 shortName 變數。

import {reallyReallyLongModuleExportName as shortName}
  from '/modules/my-module.js';

Rename multiple exports during import

Import multiple exports from a module with convenient aliases.

import {
  reallyReallyLongModuleExportName as shortName,
  anotherLongModuleName as short
} from '/modules/my-module.js';

Import a module for its side effects only

Import an entire module for side effects only, without importing anything. This runs the module's global code, but doesn't actually import any values.

import '/modules/my-module.js';

Importing defaults

It is possible to have a default export (whether it is an object, a function, a class, etc.). The import statement may then be used to import such defaults.

The simplest version directly imports the default:

import myDefault from '/modules/my-module.js';

It is also possible to use the default syntax with the ones seen above (namespace imports or named imports). In such cases, the default import will have to be declared first. For instance:

import myDefault, * as myModule from '/modules/my-module.js';
// myModule used as a namespace

或著:

import myDefault, {foo, bar} from '/modules/my-module.js';
// specific, named imports

示例

引用次要模塊以協助程式執行 AJAX JSON 請求。

模塊:file.js

function getJSON(url, callback) {
  let xhr = new XMLHttpRequest();
  xhr.onload = function () { 
    callback(this.responseText) 
  };
  xhr.open('GET', url, true);
  xhr.send();
}

export function getUsefulContents(url, callback) {
  getJSON(url, data => callback(JSON.parse(data)));
}

主要程式:main.js

import { getUsefulContents } from '/modules/file.js';

getUsefulContents('http://www.example.com',
    data => { doSomethingUseful(data); });

規範

規範 狀態 註解
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Imports' in that specification.
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
The definition of 'Imports' in that specification.
Draft  

瀏覽器相容性

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support61

16

151

60

54 — 602

No4710.1
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support No61 Yes

60

54 — 602

4710.1 No

1. From version 15: this feature is behind the Experimental JavaScript Features preference.

2. From version 54 until version 60 (exclusive): this feature is behind the dom.moduleScripts.enabled preference. To change preferences in Firefox, visit about:config.

3. From version 8: this feature is behind the --experimental-modules runtime flag.

參見

文件標籤與貢獻者

此頁面的貢獻者: iigmir, pololin, kweisamx, RichCharlie, DuckBreeder
最近更新: iigmir,