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

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

问题描述

用例很简单:我只是想导出一个名称的对象,就像导入一样。



例如:

  import反应的反应; 
export React;

但这不行。我必须写:

  import反应的反应; 
export const React = React;

但这是奇怪的。什么是正确的方法?



更新



感谢帮助和参考。我已经解决了我的问题与许多线索。我想分享一些常见的情况给我和解决方案。



导出导入



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

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



重新导出所有命名的导入



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



重新导出一些命名导入



<$ p从...中导出{a,b作为name1} $ p>



导出默认导出默认导出



  export {default} from'...'; 



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



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


解决方案

我经常在index.js文件中执行以下操作撰写几个文件:

 从'./SomeClass'导出{默认为SomeClass}; 
export {someFunction} from'./utils';
export {default as React} from'react';

这个博客条目提供了一些很好的附加示例。



重要提示



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

 从SomeClass / index.js导入SomeClassModule; 
SomeClassModule.someFunction(); //哎呀,错误

你应该这样做:


$ b $从SomeClass / index.js导入SomeClassModule,{someFunction}; b

  
someFunction(); // Ok


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

for example:

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?

UPDATED:

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.

export imports

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

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

re-export all named imports

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

re-export some named imports

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

re-export default import as default export

export {default} from '...';

re-export default import as named export

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

解决方案

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.

Important note

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

You should do this:

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

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

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