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
 |  Show 3 more comments

1 Answer 1

Reset to default 20

The 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