JavaScript函数导入不起作用 [英] JavaScript function Import not working

查看:106
本文介绍了JavaScript函数导入不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从单独的.js文件导入函数.当我声明导入命令时,页面未执行代码.但是,当我删除导入命令并执行一个简单的警报("Hello")时,该事件就会在页面上弹出.

I am trying to import a function from a separate .js file. When I declare the import command the page is not executing the code. But when I delete the import command and execute a simple alert('Hello'), that thing is popping up on the page.

项目结构
--Todo-app
---- js
------ two.js
------ main.js
---- index.html

PROJECT STRUCTURE
--Todo-app
----js
------two.js
------main.js
----index.html

Index.html

Index.html

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <script src="js/main.js"></script>
</body>
</html>

two.js

export function one() {
    return 1 + 1;
}

main.js

import { one } from 'two';
alert(one());

推荐答案

目前,任何浏览器中均未实现import和export语句.您需要使用 Babel

The import and export statements is not implemented in any browsers natively at this time. You need to use a transpiler like Babel

但是chrome和firefox可以解析此语句Uncaught SyntaxError: Unexpected token import,但不支持模块加载.

But chrome and firefox can parse this statements Uncaught SyntaxError: Unexpected token import but not support the module loading.

有关更多详细信息,请参见MDN 引用声明导入

See MDN for more détails Reference Statements import

这篇关于JavaScript函数导入不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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