简体   繁体   English

ES6 模块语法导出默认

[英]ES6 module syntax exporting default

I generally see this syntax in many places in my React app (mostly in index.js).我通常在我的 React 应用程序(主要是 index.js)中的许多地方看到这种语法。 So all that the file has is just this statement;所以文件中只有这个语句;

export {default} from './MyComponent';
export {default as STUB_DATA} from './MyStubDataFile';

What is the significance of using this syntax?使用这种语法有什么意义?

The export statement is used when creating modules to export functions, objects, primitive values from the module, so they can be used by other programs with the import statement export 语句用于创建模块以从模块中导出函数、对象、原始值,因此其他程序可以通过 import 语句使用它们

// moduleName.js
export function fun1() { ... }

// UseModule.js
import { fun1 } from './moduleName';

In react term, we can use stateless components in other components by exporting the components from their respective modules, and using it in other files.在 react 术语中,我们可以通过从各自的模块中导出组件并在其他文件中使用它来在其他组件中使用无状态组件。

export default:导出默认值:

one default export per file.每个文件一个默认导出。 When we import we have to specify a name and export like:当我们导入时,我们必须指定一个名称并导出,如:

const MyComponent = () => {}
export default MyComponent;

and import like:并像这样导入:

import MyDefaultComponent from "./MyDefaultExport";

Let say you have a folder called, core .假设您有一个名为core的文件夹。

In Core directory let's say you have below folder structure.Core目录中,假设您具有以下文件夹结构。

core
  - core.js
  - validators.js
  - hooks.js

Now let say you are going to use these in your main application.现在假设您将在主应用程序中使用这些。

So in your, app.js you can do some thing like below,因此,在您的app.js中,您可以执行以下操作,

import MyLibCore from './core/core.js';
import Validators from './core/validtors.js';

// pseudocode

// Using main method from our core library
function bootstrap() {
  const isMobile = validate();
  
  if(isMobile) {
    MyLibCore({ mobile : true });
  } else {
    MyLibCore();
  }
  
}

// using a method exposed by our core library
function validate() {
  const isMobile = Validators.isMobile();
  return isMobile;
}

Now notice the import statements of the above code.现在注意上面代码的导入语句。 Now if we share our library CORE with another developer, he may don't know the exact folder structure of our CORE libarary/directory.现在,如果我们与另一个开发人员共享我们的库CORE ,他可能不知道我们的CORE库/目录的确切文件夹结构。 So to make our library is more friendly for reuse, what we can do is, create a index.js file inside our library directory.所以为了让我们的库更易于重用,我们可以做的是,在我们的库目录中创建一个index.js文件。

So now our directory structure will looks like,所以现在我们的目录结构看起来像,

core
  - core.js
  - validators.js
  - hooks.js
  - index.js

Now inside the index.js file, we expose the list of methods/ variables/ functions to the outside.现在在 index.js 文件中,我们向外部公开了方法/变量/函数的列表。

To do that, inside the index.js file.为此,在index.js文件中。

// Expose default exports from the sub files.
export { default } from './core.js';
export { default as Validators } from './validators.js';
export { default as Hooks } from './Hooks';

// Expose any named exports
export { testRun } from './core.js' 

Now whenever we or any other developer use this core directory on the code,现在每当我们或任何其他开发人员在代码上使用这个核心目录时,

app.js

//import MyLibCore from './core/core.js';
//import Validators from './core/validtors.js';

import MyLibCore, { Validators } from './core';

Notice that, by doing this,请注意,通过这样做,

  1. The user of the core directory does not need to have knowledge about internal folder structure.核心目录的用户不需要了解内部文件夹结构。
  2. It is enough to type from './core . from './core输入就足够了。 No need to type 'index.js'无需输入“index.js”
  3. Now you can document the exported things in the index.js file to make it easy to understand.现在您可以在 index.js 文件中记录导出的内容,以便于理解。

Here's an practical example in React.这是 React 中的一个实际示例。 Check the index.js file.检查 index.js 文件。 https://github.com/mui-org/material-ui/tree/next/packages/material-ui/src/Accordion https://github.com/mui-org/material-ui/tree/next/packages/material-ui/src/Accordion

Then check here how it used.然后在这里查看它是如何使用的。 https://material-ui.com/components/accordion/ https://material-ui.com/components/accordion/

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

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