Javascript:导入一个,其余的?

Jun*_*ang 7 javascript es6-modules

对于对象const a = {b: 1, c: 2, d: 3},我们可以做解构这样的:const {b, ...rest} = a

我想知道imports是否也可以。

说我有一个文件file1.js

// file1.js
export const a = 1;
export const b = 2;
export const c = 3;
Run Code Online (Sandbox Code Playgroud)

我可以通过导入一个和其他像解构一样从这个文件导入吗?

// file2.js
import {a, ...rest} from "file1";
Run Code Online (Sandbox Code Playgroud)

T.J*_*der 6

不直接,没有import形式。

您可以做的是导入模块的模块命名空间对象,然后对其使用解构:

import * as file1 from "file1";
const {a, ...rest} = file1;
Run Code Online (Sandbox Code Playgroud)

FWIW,该规范在此处提供import表单示例列表。它没有显示您可以组合和不能组合的内容,但这些是基本形式。

  • (FWIW,我在我的新书第 13 章中深入探讨了模块以及“导入”和“导出”的可能形式;如果您感兴趣,请在我的个人资料中链接。) (2认同)