ES6,如何在一行中导出导入的模块?

问题描述 投票:0回答:7

如果可能的话,我想要以下内容,但用一行:

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

我尝试了以下方法,但似乎不起作用:

  • export Module from './Module/Module;
react-native ecmascript-6 import
7个回答
287
投票
export {default as Module} from './Module/Module';

是标准的 ES6 方式,只要您不需要

Module
也可以在进行导出的模块内使用。

export Module from './Module/Module';

是建议的 ESnext 方法,但只有在您现在在 Babel 中启用它时才有效。


45
投票

我不知道为什么,但这对我有用:

组件/index.js:

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

export {Component, Component2, Component3, Component4};

我像这样导入导出:

import {Component, Component2, Component3, Component4} from '../components';

38
投票

请注意,您还可以重新导出模块中的所有内容:

export * from './Module/Module';

21
投票

对于 React Native 组件,此语法对我有用:

export {default} from 'react-native-swiper';

0
投票
// Service
// ...
export const paymentService = new PaymentService()

// Entry services/index.js file
export * from './paymentService'

// use it like
import { paymentService } from './services/'

0
投票
export {Module as default} from '/path'

这对我有用。


-3
投票

因此,我发现这对于立即导出功能非常有效,在

index.js
目录的根目录下有一个
components
以便于引用:

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

module.exports = {
  Component,
  ComponentTwo
};

您需要使用

module.exports

© www.soinside.com 2019 - 2024. All rights reserved.