Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ES6仅从文件夹导入一些文件_Javascript_Ecmascript 6_Es6 Modules_Ecmascript 2016 - Fatal编程技术网

Javascript ES6仅从文件夹导入一些文件

Javascript ES6仅从文件夹导入一些文件,javascript,ecmascript-6,es6-modules,ecmascript-2016,Javascript,Ecmascript 6,Es6 Modules,Ecmascript 2016,也许我完全忘记了ES6导入/导出的工作原理,但我希望能够做到以下几点: import { b } from 'folder'; 和不从文件夹索引文件加载所有其他导出的文件 这就是我目前拥有的: ├── folder │   ├── file1.js │   ├── file2.js │   ├── file3.js │   └── index.js ├── test.js //file1.js: console.log("in file1"); export const

也许我完全忘记了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
如何从
文件夹导入各种文件,而不必使用特定的文件导入:

import { b } from "./folder/file2";
因为我可能需要从那个文件夹导入很多文件,我希望我可以使用导入解构

因此,不是:

import { b } from "./folder/file2";
import { c } from "./folder/file3";
我希望能够使用:

import { b, c } from "./folder";

但是不必从文件夹中加载其他文件(在本例中为a)。

如何从文件夹中导入各种文件而不必使用特定的文件导入,您不能。导出是在运行时确定的,这意味着需要对
index.js
文件进行评估,以确定应该导出什么

但是,您可以以这样一种方式来设计文件,即对其进行评估不会运行任何业务代码

例如:

//file1.js:
导出常量模块1=()=>{
console.log(“在文件1中”);
返回({
a:3+4,
});
}
//test.js
从“/”文件夹导入{Module1}”;
常数{a}=Module1();

如何从文件夹中导入各种文件而不必使用特定的文件导入
,您不能。导出是在运行时确定的,这意味着需要对
index.js
文件进行评估,以确定应该导出什么

但是,您可以以这样一种方式来设计文件,即对其进行评估不会运行任何业务代码

例如:

//file1.js:
导出常量模块1=()=>{
console.log(“在文件1中”);
返回({
a:3+4,
});
}
//test.js
从“/”文件夹导入{Module1}”;
常数{a}=Module1();
此处的类似问题(您可以使用此线程中提到的一些变通方法):此处的类似问题(您可以使用此线程中提到的一些变通方法):