导入属性
🌐 Import attributes
版本历史
| 版本 | 变更 |
|---|---|
| v21.0.0, v20.10.0, v18.20.0 | Switch from Import Assertions to Import Attributes. |
| v17.1.0, v16.14.0 | 新增于: v17.1.0, v16.14.0 |
导入属性 是一种用于模块导入语句的内联语法,用于在模块说明符旁传递更多信息。
import fooData from './foo.json' with { type: 'json' };
const { default: barData } =
await import('./bar.json', { with: { type: 'json' } }); Node.js 仅支持 type 属性,并支持以下值:
🌐 Node.js only supports the type attribute, for which it supports the following values:
属性 type | 所需用于 |
|---|---|
'json' | JSON 模块 |
在导入 JSON 模块时,type: 'json' 属性是必须的。
🌐 The type: 'json' attribute is mandatory when importing JSON modules.