如何将Javascript导入其他JavaScript?

时间:2019-03-22 15:31:37

标签: javascript import

我想在Javascript中使用import,但无法使其正常工作(Fedora 28上的Firefox 65.0.2)。

我的HTML中有这个

<!DOCTYPE html>
<html>
   <head>
     <script src="js/App.js" type="module"></script>
      <script type="text/javascript">        
            $(function() {
                application = new App(true);
            });
        </script>
   </head>
</html>

以及在我的js/App.js中:

import "js/Helpers.js"
class App {
    constructor(needsLogin) {
    }
}

js/Helpers.js

class Helpers {
}

但是,在运行时,出现此错误:

  

ReferenceError:未定义应用

我尝试在import调用上方添加new App(),但这给了我

  

SyntaxError:导入声明只能出现在模块的顶层

如何在我的Helpers.js中导入App.js

0 个答案:

没有答案