ES6导入/导出是否需要“.js”延期? [英] Does ES6 import/export need ".js" extension?

查看:130
本文介绍了ES6导入/导出是否需要“.js”延期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装了chrome beta - 版本60.0.3112.24(官方版本)测试版(64位)

I installed chrome beta - Version 60.0.3112.24 (Official Build) beta (64-bit)

在chrome:// flags / I enabled'实验网络平台功能'(参见 https://jakearchibald.com/2017/es-modules-in-browsers

In chrome://flags/ I enabled 'Experimental Web Platform features' (see https://jakearchibald.com/2017/es-modules-in-browsers)

然后我尝试了:

<script type="module" src='bla/src/index.js'></script>

其中index.js的行如下:

where index.js has a line like:

export { default as drawImage } from './drawImage';

这是指现有的文件drawImage.js

This refer to an existing file drawImage.js

我在控制台得到的是错误

what I get in the console is error in

GET http://localhost/bla/src/drawImage 

如果我更改导出并添加.js扩展名,它可以正常工作。

If I change the export and add ".js" extension it works fine.

这是一个chrome bug还是ES6在这种情况下要求扩展?

Is this a chrome bug or does ES6 demands the extension in this case ?

webpack也可以在没有扩展名的情况下构建它!

Also webpack builds it fine without the extension !

推荐答案

不,模块不关心扩展。它只需要是一个解析为源文件的名称。

No, modules don't care about extensions. It just needs to be a name that resolves to a source file.

在你的情况下, http:// localhost / bla / src / drawImage 不是文件,而 http://localhost/bla/src/drawImage.js 是,所以这就是错误来自的地方。例如,您可以将服务器配置为忽略扩展名。 Webpack也是如此。

In your case, http://localhost/bla/src/drawImage is not a file while http://localhost/bla/src/drawImage.js is, so that's where there error comes from. You can configure your server to ignore the extension, for example. Webpack does the same.

这篇关于ES6导入/导出是否需要“.js”延期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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