ES6, how can you export an imported module in a si

2019-01-07 15:28发布

I'd like to the following but with a single line, if possible:

  • import Module from './Module/Module;
  • export Module;

I tried the following but it doesn't seem to work:

  • export Module from './Module/Module;

5条回答
贪生不怕死
2楼-- · 2019-01-07 16:00
export {default as Module} from './Module/Module';

is the standard ES6 way, as long as you don't need Module to also be available inside the module doing the exporting.

export Module from './Module/Module;

is a proposed ESnext way to do it, but that only works if you've enabled it in Babel for now.

查看更多
不美不萌又怎样
3楼-- · 2019-01-07 16:01

So, here is the answer!

module.exports.your_module = require("your_module");

in my case i needed to bundle all of my react components in to library. worked fine as well as packing it as a library in webpack.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-07 16:08

I don't know why but just this works for me :

index.jsx:

import Component from './Component';
import Component2 from './Component2';
import Component3 from './Component3';
import Component4 from './Component4';

export {Component, Component2, Component3, Component4};

I import the exports like this :

import {Component, Component2, Component3, Component4} from '../componets/index';
查看更多
We Are One
5楼-- · 2019-01-07 16:09

So, I've found this to work quite well for the immediate export functionality of having an index.js at the root of the components directory for easy referencing:

import Component from './Component/Component'
import ComponentTwo from './ComponentTwo/ComponentTwo'

module.exports = {
  Component,
  ComponentTwo
};

You need to use module.exports.

查看更多
劳资没心,怎么记你
6楼-- · 2019-01-07 16:21

Please note you can also re-export everything from a module:

export * from './Module/Module';
查看更多
登录 后发表回答