如何在 ES6 中导出导入的对象? [英] How to export imported object in ES6?

查看:55
本文介绍了如何在 ES6 中导出导入的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用例很简单:我只想用导入时的名称导出一个对象.

The use case is simple: I just want to export an object with the name just as it was imported.

例如:

import React from 'react';
export React;

但这不起作用.我必须写:

but this does not work. I have to write:

import React from 'react';
export const React = React;

但这很奇怪.这样做的正确方法是什么?

But this is odd. What is the right way to do this?

更新:

感谢您的帮助和参考.我已经用很多线索解决了我的问题.我想分享一些我的常见案例和解决方案.

Thanks for helps and references. I have solved out my problem with many clues. I'd like to share some common cases for me and the solutions.

import d, {obj} from '...';

export {obj, d};
export {obj as name1, d as name2};

重新导出所有命名的导入

export * from '...';
export * as name1 from '...';

重新导出一些命名导入

export {a, b as name1} from '...';

重新导出默认导入为默认导出

export {default} from '...';

将默认导入重新导出为命名导出

export {default as name1} from '...';

推荐答案

我经常在由多个文件组成的 index.js 文件中执行以下操作:

I often do the following in index.js files that compose several files:

export {default as SomeClass} from './SomeClass';
export {someFunction} from './utils';
export {default as React} from 'react';

博客条目提供了一些不错的附加示例.

This blog entry provides some nice additional examples.

你应该知道这个 eslint-rule 访问这些导出的导入时.基本上,在另一个文件中,您不应该:

You should be aware this eslint-rule when accessing these exported imports. Basically, in another file, you shouldn't:

import SomeClassModule from 'SomeClass/index.js';
SomeClassModule.someFunction(); // Oops, error

你应该这样做:

import SomeClassModule, {someFunction} from 'SomeClass/index.js';
someFunction(); // Ok

这篇关于如何在 ES6 中导出导入的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆