为什么无法使用“import ... as”导入默认导出与BabelJS [英] Why cant I import a default export with "import ... as" with BabelJS

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

问题描述

在BabelJS版本5.6.4中,我似乎无法导入...。以下是我想要做的例子:



在文件'test.js'中:

  export default class Test {}; 

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

 导入从./test导入Test2作为Test2; 

我也尝试过:

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

即使在这里没有提到:
http://babeljs.io/docs/learn-es2015/#modules



这里只使用非默认语法中的括号:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import



<有没有人成功完成了?



编辑:绝对是因为默认值关键字。所以,在这种情况下,问题就变成了,有没有人有链接到文档,指出我不能够默认导入别名? ECMA或Babel。

解决方案

您可以通过



导入默认导出pre> 从'./test'导入Test2;

  import {default as Test2} from'./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天全站免登陆