ES6 模块抛出意外的令牌错误
ES6 Module throwing unexpected token error
遇到这个问题让我大吃一惊,但我们开始吧。我在控制台中收到错误消息:
Uncaught SyntaxError: Unexpected token {
在我的 index.js 文件中的这一行代码中:
import { CountUp } from '/js/count-up.js';
该行是文件中的第一行,我在 HTML 页面上的脚本标记如下所示:
<script type="module" src='/js/count-up.js'></script>
<script src='index.js'></script>
我在使用 Macbook pro 运行 Mojave,我在 Chrome 73.
我真的不知道我哪里出错了,为什么我在简单的 es6 模块导入中遇到未捕获的语法错误?
<script src='index.js'>
缺少 type="module"
属性,因此它试图在不支持 ES6 模块语法(import
需要)的情况下加载它。
旁白:
删除:
<script type="module" src='/js/count-up.js'></script>
您只需要使用 <script>
元素将入口点加载到您的 JS 程序。 /js/count-up.js
使用 import { CountUp } from '/js/count-up.js';
.
加载
在脚本声明中使用属性 "type":
https://github.com/inorganik/countUp.js/
"Include in your html. Notice the type attribute"
<script src="./js/countUp.min.js" type="module"></script>
遇到这个问题让我大吃一惊,但我们开始吧。我在控制台中收到错误消息:
Uncaught SyntaxError: Unexpected token {
在我的 index.js 文件中的这一行代码中:
import { CountUp } from '/js/count-up.js';
该行是文件中的第一行,我在 HTML 页面上的脚本标记如下所示:
<script type="module" src='/js/count-up.js'></script>
<script src='index.js'></script>
我在使用 Macbook pro 运行 Mojave,我在 Chrome 73.
我真的不知道我哪里出错了,为什么我在简单的 es6 模块导入中遇到未捕获的语法错误?
<script src='index.js'>
缺少 type="module"
属性,因此它试图在不支持 ES6 模块语法(import
需要)的情况下加载它。
旁白:
删除:
<script type="module" src='/js/count-up.js'></script>
您只需要使用 <script>
元素将入口点加载到您的 JS 程序。 /js/count-up.js
使用 import { CountUp } from '/js/count-up.js';
.
在脚本声明中使用属性 "type":
https://github.com/inorganik/countUp.js/
"Include in your html. Notice the type attribute"
<script src="./js/countUp.min.js" type="module"></script>