如何在 javascript/es6 中以相同的名称导入两个类? [英] How to import two classes by the same name in javascript/es6?

查看:7
本文介绍了如何在 javascript/es6 中以相同的名称导入两个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文件中有这两个导入语句:

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?

推荐答案

大概 component/Dataactions/Data 都有 default 导出而不是命名出口?像这样:

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 {}

然后你需要使用大括号和 as 来指定源和目标名称:

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屋!

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