为什么我不能使用"import ... as"导入默认导出?与BabelJS [英] Why can't I import a default export with "import ... as" with BabelJS

查看:225
本文介绍了为什么我不能使用"import ... as"导入默认导出?与BabelJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在BabelJS的5.6.4版本中,我似乎无法将...导入为".以下是我正在尝试做的事的示例:

在文件"test.js"中:

export default class Test {};

在文件"test2.js"中(在同一目录中):

import Test as Test2 from './test';

我也曾尝试这样做:

import {Test as Test2} from './test';

即使在这里它什么也没说: http://babeljs.io/docs/learn-es2015/#modules

并且仅在此处使用非默认语法的方括号: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/import

有人成功做到了吗?

编辑:这完全是因为default关键字.因此,在这种情况下,问题就来了,是否有人可以链接到说明我不应该为默认导入别名的文档? ECMA或Babel.

解决方案

您可以通过以下任一方式导入默认导出

import Test2 from './test';

import {default as Test2} from './test';

默认导出没有Test作为您需要别名的名称-您只需要在所需名称下导入默认值即可.

到目前为止,我发现的最好的文档是文章 ECMAScript 6模块: Axel Rauschmayers博客中的最​​终语法 .

In version 5.6.4 of BabelJS, I seemingly cannot "import ... as." Here are examples of what I am trying to do:

In file 'test.js':

export default class Test {};

In file 'test2.js' (in the same directory):

import Test as Test2 from './test';

I have also tried to do:

import {Test as Test2} from './test';

Even though it says nothing about that here: http://babeljs.io/docs/learn-es2015/#modules

And only uses brackets in the non-default syntax here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

Has anyone done this successfully?

EDIT: It is absolutely because of the default keyword. So, in this case, the question becomes, does anyone have any links to documentation that states that I should not be able to alias a default import? ECMA or Babel.

解决方案

You can import the default export by either

import Test2 from './test';

or

import {default as Test2} from './test';

The default export doesn't have Test as a name that you would need to alias - you just need to import the default under the name that you want.

The best docs I've found so far is the article ECMAScript 6 modules: the final syntax in Axel Rauschmayers blog.

这篇关于为什么我不能使用"import ... as"导入默认导出?与BabelJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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