admin管理员组文章数量:1199960
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.
PROJECT STRUCTURE
--Todo-app
----js
------two.js
------main.js
----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());
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.
PROJECT STRUCTURE
--Todo-app
----js
------two.js
------main.js
----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());
Share
Improve this question
asked Jan 15, 2017 at 14:06
s.ns.n
7031 gold badge9 silver badges18 bronze badges
8
- Try to use './two' – Mustafa Mamun Commented Jan 15, 2017 at 14:10
- Can you see in the console any errors messages with the import statement? – NotBad4U Commented Jan 15, 2017 at 14:11
- @MustafaMamun content of the two.js is given above – s.n Commented Jan 15, 2017 at 14:11
- Sorry have not seen it before corrected it. – Mustafa Mamun Commented Jan 15, 2017 at 14:13
- 3 ES6 modules aren't supported in browsers (with the exception of Edge). The code needs to be transpiled with Babel. – Estus Flask Commented Jan 15, 2017 at 14:25
1 Answer
Reset to default 20The import and export statements is not implemented in any browsers natively at this time. You need to use a transpiler like Babel
But chrome and firefox can parse this statements Uncaught SyntaxError: Unexpected token import
but not support the module loading.
See MDN for more détails Reference Statements import
本文标签: htmlJavaScript function Import not workingStack Overflow
版权声明:本文标题:html - JavaScript function Import not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738594152a2101690.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论