【问题标题】:Export multiple classes in ES6 modules在 ES6 模块中导出多个类
【发布时间】:2016-11-15 09:09:18
【问题描述】:

我正在尝试创建一个导出多个 ES6 类的模块。假设我有以下目录结构:

my/
└── module/
    ├── Foo.js
    ├── Bar.js
    └── index.js

Foo.jsBar.js 分别导出一个默认的 ES6 类:

// Foo.js
export default class Foo {
  // class definition
}

// Bar.js
export default class Bar {
  // class definition
}

我目前的index.js 设置如下:

import Foo from './Foo';
import Bar from './Bar';

export default {
  Foo,
  Bar,
}

但是,我无法导入。我希望能够做到这一点,但找不到类:

import {Foo, Bar} from 'my/module';

在一个 ES6 模块中导出多个类的正确方法是什么?

【问题讨论】:

  • 只使用export而不使用默认值
  • 您只能有一个default 导出。想象一下,如果有人试图做import SomeClass from 'my/module'。这将自动从该路径导入default 模块。如果那里有多个默认导出,它怎么知道要导入哪一个?

标签: javascript module export ecmascript-6 babeljs


【解决方案1】:

在你的代码中试试这个:

import Foo from './Foo';
import Bar from './Bar';

// without default
export {
  Foo,
  Bar,
}

顺便说一句,你也可以这样做:

// bundle.js
export { default as Foo } from './Foo'
export { default as Bar } from './Bar'
export { default } from './Baz'

// and import somewhere..
import Baz, { Foo, Bar } from './bundle'

使用export

export const MyFunction = () => {}
export const MyFunction2 = () => {}

const Var = 1;
const Var2 = 2;

export {
   Var,
   Var2,
}


// Then import it this way
import {
  MyFunction,
  MyFunction2,
  Var,
  Var2,
} from './foo-bar-baz';

export default 的区别在于您可以导出某些内容,并在导入时应用名称:

// export default
export default class UserClass {
  constructor() {}
};

// import it
import User from './user'

【讨论】:

  • 我在构建 export Foo from './Foo'; export Bar from './Bar' 时收到 Unexpected token 错误
  • @inostia 注意,这是 ES6 语法,你可能需要 "babel" 来支持它
  • 我正在使用 babel。使用 webpack 编译时出现该错误。我想我需要做类似export { default as Foo } from './Foo'; 的事情。我在别处见过
  • @inostia 我也遇到这种情况,需要export { default as Foo } from './Foo'; 才能实际导出它。
【解决方案2】:

希望这会有所帮助:

// Export (file name: my-functions.js)
export const MyFunction1 = () => {}
export const MyFunction2 = () => {}
export const MyFunction3 = () => {}

// if using `eslint` (airbnb) then you will see warning, so do this:
const MyFunction1 = () => {}
const MyFunction2 = () => {}
const MyFunction3 = () => {}

export {MyFunction1, MyFunction2, MyFunction3};

// Import
import * as myFns from "./my-functions";

myFns.MyFunction1();
myFns.MyFunction2();
myFns.MyFunction3();


// OR Import it as Destructured
import { MyFunction1, MyFunction2, MyFunction3 } from "./my-functions";

// AND you can use it like below with brackets (Parentheses) if it's a function 
// AND without brackets if it's not function (eg. variables, Objects or Arrays)  
MyFunction1();
MyFunction2();

【讨论】:

    【解决方案3】:

    @webdeb 的回答对我不起作用,我在使用 Babel 编译 ES6 时遇到了unexpected token 错误,执行命名默认导出。

    这对我有用,但是:

    // Foo.js
    export default Foo
    ...
    
    // bundle.js
    export { default as Foo } from './Foo'
    export { default as Bar } from './Bar'
    ...
    
    // and import somewhere..
    import { Foo, Bar } from './bundle'
    

    【讨论】:

      【解决方案4】:
      // export in index.js
      export { default as Foo } from './Foo';
      export { default as Bar } from './Bar';
      
      // then import both
      import { Foo, Bar } from 'my/module';
      

      【讨论】:

        【解决方案5】:

        对于同一js文件中的多个classes,从@wordpress/element扩展Component,您可以这样做:

        // classes.js
        import { Component } from '@wordpress/element';
        
        const Class1 = class extends Component {
        }
        
        const Class2 = class extends Component {
        }
        
        export { Class1, Class2 }
        

        并将它们导入另一个js 文件:

        import { Class1, Class2 } from './classes';
        

        【讨论】:

          【解决方案6】:

          要导出类的实例,您可以使用以下语法:

          // export index.js
          const Foo = require('./my/module/foo');
          const Bar = require('./my/module/bar');
          
          module.exports = {
              Foo : new Foo(),
              Bar : new Bar()
          };
          
          // import and run method
          const {Foo,Bar} = require('module_name');
          Foo.test();
          

          【讨论】:

          • 这不是 ES6 语法
          猜你喜欢
          • 1970-01-01
          • 2016-01-05
          • 1970-01-01
          • 1970-01-01
          • 2020-11-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-01
          相关资源
          最近更新 更多