ES6 Javascript在多个名称下导入相同的js文件 [英] ES6 Javascript Import same js file under multiple names

查看:414
本文介绍了ES6 Javascript在多个名称下导入相同的js文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ES6,我想将同一个文件导入为两个不同的名称。

I'm working with ES6 and I want to import the same file as 2 different names.

import Contact from './Grid'
import Account from './Grid'

有没有办法我可以通过联系人和帐户命名网格而不用webpack多次导入它?

Is there a way that I can have grid named by both contact and account without webpack importing it multiple times?

推荐答案


webpack多次导入?

without webpack importing it multiple times?

使用两个单独的import语句将不会多次执行该文件。文件加载一次后,其缓存的导出值将用于以后的调用。鉴于此,将其归为一个陈述的唯一理由是潜在的可读性改进。也就是说,为了回答你的问题,你可以做到

Using two separate import statements will not execute the file multiple times. Once a file has been loaded once, its exported values are cached for use with later calls. Given that, the only reason to group it into one statement would be potential readability improvements. That said, to answer your question, you can do

import { 
  default as Contact,
  default as Account,
} from './Grid';

如果你愿意的话。您也可以这样做

if you want. You could also potentially do

import Contact from './Grid'
const Account = Contact;

请注意,在案件中它不会完全完全模块中存在循环依赖的地方。

just note that it doesn't do quite the same thing in cases where there are circular dependencies in your modules.

这篇关于ES6 Javascript在多个名称下导入相同的js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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