编程语言
首页 > 编程语言> > javascript – Chrome 61:意外的令牌导入

javascript – Chrome 61:意外的令牌导入

作者:互联网

运行Chrome 61,导入为supposed to support module loading.

确实保罗的demo对我有用.但是,当我自己尝试时,我得到一个JS错误“意外的令牌导入”. Chrome似乎在导入时不愿意:

的test.html

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

test.js:

import {hello} from './something.js'
console.log(hello())

something.js

export {hello}
function hello() {
    return "hello world"
}

为什么Chrome不理解“导入”

解决方法:

对于那些想要确切知道对我有用的人来说,这可能是上面几个答案的组合.我还必须通过在URL栏中键入chrome:// flags并搜索“import”来启用Chrome的ES6导入功能.

首先是HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Testing JavaScript Stuff</title>
</head>
<body>
    <script type="module">
        import { circleArea, squareArea } from './CalcArea.js';

        console.log(circleArea(2));
        console.log(squareArea(2));
    </script>
</body>
</html>

因此,您可以看到只需在脚本标记中添加“模块”类型,然后在下面执行导入.对于我的测试,CalcArea.js文件是这样的:

const circleArea = r => 3.14 * (r ** 2);

const squareArea = s => s * s;

export {circleArea, squareArea};

标签:es6-modules,javascript,google-chrome
来源: https://codeday.me/bug/20190928/1828464.html