Skip to content

Commit 1bd1156

Browse files
committed
docs(module): add import attributes
1 parent a7335cb commit 1bd1156

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

docs/module.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,30 @@ import * as ns from "mod";
558558
export {ns};
559559
```
560560

561+
## import 属性
562+
563+
ES2025 引入了“[import 属性](https://github.com/tc39/proposal-import-attributes)”(import attributes),允许为 import 命令设置属性,主要用于导入非模块的代码,比如 JSON 数据、WebAssembly 代码、CSS 代码。
564+
565+
目前,只支持导入 JSON 数据。
566+
567+
```javascript
568+
// 静态导入
569+
import configData from './config-data.json' with { type: 'json' };
570+
571+
// 动态导入
572+
const configData = await import(
573+
'./config-data.json', { with: { type: 'json' } }
574+
);
575+
```
576+
577+
上面代码中,import 命令使用 with 子句,指定一个属性对象。这个属性对象目前只有一个 type 属性,它的值就是导入代码的类型,现在只能设置为`json`一个值。
578+
579+
export 命令与 import 命令写在一起,形成一个再导出语句时,也可以使用 import 属性。
580+
581+
```javascript
582+
export { default as config } from './config-data.json' with { type: 'json' };
583+
```
584+
561585
## 模块的继承
562586

563587
模块之间也可以继承。

0 commit comments

Comments
 (0)