Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

ecmascript 6 - How to import two classes by the same name in javascript/es6?

I have these two import statements in file:

import Data from 'component/Data.js';
import Data from 'actions/Data.js';

Both files contain a class named Data.

How can I specify which is which? How can I avoid name clash?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Presumably component/Data and actions/Data both have default exports rather than named exports? Like this:

export default class Data {}

If that's the case, then the importer can call the variables whatever they like:

import Data1 from 'component/Data.js';
import Data2 from 'actions/Data.js';

If they are named exports:

export class Data {}

Then you need to use braces along with as to specify the source and target names:

import { Data as Data1 } from 'component/Data.js';
import { Data as Data2 } from 'actions/Data.js';

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...