简体   繁体   English

通过一系列文件了解ES6中的导入/导出

[英]Understanding import/export in ES6 with a chain of files

I'm having a bit of a trouble understanding export/import mechanism when I have 3 files, each including the previous one. 当我有3个文件时,我在理解导出/导入机制时遇到了一些麻烦,每个文件都包含前一个文件。

Example: 例:

//fileA.js:
export default MyClass {}

//fileB.js:
import MyClass from './fileA.js';

//fileC.js:
import './fileB.js';

My expectation is that MyClass is then available in fileC, but it looks like it's just not the case. 我的期望是,然后在fileC中可以使用MyClass ,但看起来情况并非如此。 Can you advise? 你能建议吗?

Do like below: 如下所示:

//fileA.js:
export default MyClass {}

//fileB.js:
export { default as MyClass } from './fileA'

//fileC.js:
import { MyClass } from './fileB'

When you import a module like this: 导入这样的模块时:

//fileC.js:
import './fileB.js';

it doesn't actually import any values. 它实际上并不导入任何值。 It will execute the global code in that module for side-effects, but does not import the values themselves. 它将在该模块中执行副作用的全局代码,但不会自己导入值。 In order to do what you want to do, you will need to import module values in every file you want to use them. 为了执行您想要执行的操作,您需要在要使用它们的每个文件中导入模块值。

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Import_a_module_for_its_side_effects_only 请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Import_a_module_for_its_side_effects_only

In your following code: 在您的以下代码中:

//fileA.js:
export default MyClass {}

//fileB.js:
import MyClass from './fileA.js';

//fileC.js:
import './fileB.js';

Myclass is only going to be available in fileB because this is directly importing this. Myclass只能在fileB中使用,因为这是直接导入它。 Your 3th line of code is not going to load in fileA. 你的第3行代码不会加载到fileA中。 Just because FileA is loaded in FileB doesn't mean that the dependency gets transferred when we import B in FileC 仅仅因为在FileB中加载FileA并不意味着当我们在FileC中导入B时依赖性被转移

To make it avialable, we have to import it again in file C like we did in file B: 为了使其成为可用的,我们必须像在文件B中那样在文件C中再次导入它:

import MyClass from './fileA.js';

Other import export syntax: 其他import export语法:

When one file exports multiple things (without the default in front) we can import it in the using named export for example: 当一个文件导出多个东西(前面没有默认值)时,我们可以在使用命名导出中导入它,例如:

export class1 {}

export class2 {}

We are exporting 2 classes, we could import them using the following syntax: 我们正在导出2个类,我们可以使用以下语法导入它们:

import {class1, class2} from './fileA.js';

Keep in mind that the names now have to match the names of the exports, class1 and class2 in this case. 请记住,在这种情况下,名称现在必须匹配导出的名称, class1class2

We could also import all of the exports of a file in the following manner: 我们还可以通过以下方式导入文件的所有导出:

import * as classes from './fileA.js';

classes.class1 // the way to access class1

This syntax put all exports on an object of which we can determine the name (after the as keyword). 此语法将所有导出放在我们可以确定名称的对象上(在as关键字之后)。 We then can access the exports like we normally access properties of an object. 然后,我们可以访问导出,就像我们通常访问对象的属性一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM