Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
609 views
in Technique[技术] by (71.8m points)

import - How to export imported object in ES6?

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 '...';
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...