立即ES6导出(React,Webpack,Babel)

3

在一个目录的根目录下(例如components/containers/),我有一个index.jsx文件,它立即导出所有组件,以便我可以像这样导入它们:

    import {SampleOne, SampleTwo} from '../components'.

然而,根目录下的index.jsx文件无法使用以下方法:

    import SampleOne from './SampleOne/SampleOne';
    import SampleTwo from './Sample/SampleTwo';

    export default {
      SampleOne,
      SampleTwo  
    };

因此,我将其转换为以下内容(基本上相同):
    export {default as SampleOne} from './SampleOne/SampleOne';
    export {default as SampleTwo} from './SampleTwo/SampleTwo';

这个方案可行,但我收到了一个警告:

    Can't make class (SampleOne & SampleTwo) hot reloadable due to being read-only.
    To fix this you can try two solutions. First, you can exclude files or directories
    (for example, /node_modules/) using 'exclude' option in loader configuration.
    Second, if you are using Babel, you can enable loose mode for `es6.modules` using
    the 'loose' option. See: http://babeljs.io/docs/advanced/loose/
    and http://babeljs.io/docs/usage/options/

1
只能有一个 default 导出。 - a better oliver
@zeroflagL:你看到不只一个了吗? - Felix Kling
你有问题吗? - Felix Kling
@FelixKling 我对第一个例子中的速记语法和“基本相同”的说法感到困惑。再看一眼,两个版本是不同的。第一个版本将单个对象导出为默认值,而第二个版本具有两个命名导出项。 - a better oliver
是的,使用默认方式导出的语法与其他方式相同。实际上,正如提到的那样,其中一个可以正常工作,而另一个则不行。 - Detuned
2个回答

1
经过一番艰苦努力,我们替换了


export {default as SampleOne} from './SampleOne/SampleOne';
export {default as SampleTwo} from './SampleTwo/SampleTwo';

带有。
import {default as SampleOne} from './SampleOne/SampleOne';
import {default as SampleTwo} from './SampleTwo/SampleTwo';

export {SampleOne, SampleTwo};

而且这对我们来说似乎是有效的。


-1

我遇到了相同的问题,因为我在每个模块中使用index.js来导出所有子模块,就像这样:

export {default as SampleOne} from './SampleOne/SampleOne';
export {default as SampleTwo} from './SampleTwo/SampleTwo';

根据react-hot-loader存储库中的讨论,您可以选择忽略它(因为它仍然被加载),或者不重新导出模块。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接