浏览器中的ES6模块:Uncaught SyntaxError:意外的令牌导入 [英] ES6 modules in the browser: Uncaught SyntaxError: Unexpected token import

查看:406
本文介绍了浏览器中的ES6模块:Uncaught SyntaxError:意外的令牌导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ES6 (ECMAScript 6)的新手,我想在浏览器中使用其模块系统.我读到Firefox和Chrome支持ES6,但是使用export

I'm new to ES6 (ECMAScript 6), and I'd like to use its module system in the browser. I read ES6 is supported by Firefox and Chrome, but I'm getting the following error using export

Uncaught SyntaxError: Unexpected token import

我有一个test.html文件

I have a test.html file

<html>
    <script src="test.js"></script>
<body>
</body>
</html>

和一个test.js文件

and a test.js file

'use strict';

class Test {

    static hello() {
        console.log("hello world");
    } 
}

export Test;    

为什么?

推荐答案

许多现代浏览器现在都支持ES6模块.只要您使用<script type="module" src="...">导入脚本(包括应用程序的入口点),它就可以工作.

Many modern browsers now support ES6 modules. As long as you import your scripts (including the entrypoint to your application) using <script type="module" src="..."> it will work.

有关更多详细信息,请参见 caniuse.com : https://caniuse.com/#feat=es6-module

Take a look at caniuse.com for more details: https://caniuse.com/#feat=es6-module

这篇关于浏览器中的ES6模块:Uncaught SyntaxError:意外的令牌导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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