如何在javascript / es6中同名导入两个类? [英] How to import two classes by the same name in javascript/es6?
本文介绍了如何在javascript / es6中同名导入两个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个import语句在文件中:
I have these two import statements in file:
import Data from 'component/Data.js';
import Data from 'actions/Data.js';
这两个文件都包含一个名为 Data
的类。
Both files contain a class named Data
.
如何指定哪个是哪个?如何避免名称冲突?
How can I specify which is which? How can I avoid name clash?
推荐答案
大概是组件/数据
actions / Data
都具有默认导出而不是命名导出?像这样:
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';
如果它们被命名为export:
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';
这篇关于如何在javascript / es6中同名导入两个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文