【发布时间】:2020-10-16 22:01:13
【问题描述】:
也许我完全忘记了 ES6 导入/导出是如何工作的,但我希望能够做这样的事情:
import { b } from 'folder';
并且不从文件夹索引文件中加载所有其他导出的文件。
这是我目前拥有的:
├── folder
│ ├── file1.js
│ ├── file2.js
│ ├── file3.js
│ └── index.js
├── test.js
//file1.js:
console.log("in file1");
export const a = 3 + 4;
//file2.js:
console.log("in file2");
export const b = 3 + 5;
//file3.js:
console.log("in file3");
export const c = 3 + 6;
//index.js:
export * from "./file1";
export * from "./file2";
export * from "./file3";
//test.js:
import { b } from "./folder";
console.log(`this is ${b}`);
问题在于,在执行此操作时,它还会加载其他两个文件(a 和 c),如输出所示:
in file1
in file2
in file3
this is 8
如何从folder导入各种文件,而无需使用特定的文件导入:
import { b } from "./folder/file2";
因为我可能需要从那个文件夹中导入很多文件,我希望我可以使用导入解构。
所以而不是:
import { b } from "./folder/file2";
import { c } from "./folder/file3";
我希望能够使用:
import { b, c } from "./folder";
但不必从文件夹中加载其他文件(在本例中为 a)。
【问题讨论】:
-
这里有类似的问题(你可以利用这个线程中提到的一些解决方法):stackoverflow.com/questions/29722270/…
标签: javascript ecmascript-6 es6-modules ecmascript-2016