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

时间:2019-01-19 09:19:01

标签: javascript flask es6-modules

我是Web编程的新手,我正在尝试一个非常简单的示例,以了解其工作原理。

html file
<html>
<head>
<meta charset="ISO-8859-1">

<script type="text/javascript" src="/static/js/AddButton.js"></script>
<script type="text/javascript" src="/static/js/SendRequest.js"></script>

<title>{{title}}</title>


</head>
<body>

<button type="button" onclick="AddForm()"> + </button>

<div id = "newFormHere"></div>


<form action="javascript: SendFetchRequest()" id ="Form2">
    Ticker2:
        <input type="text" id="Ticker2" value="GOOG">
        <input type="submit" value="Submit">
        <div id = "FetchRequestOutput"></div>
</form>

</body>
</html>

我有一个向服务器发送请求的表单(Form2)和一个向页面添加更多表单的按钮。

SendRequest.js (exporting functions)
export {SendFetchRequest}



function SendFetchRequest()
{
    var Ticker = document.getElementById("Ticker2");
    fetch("action_page" + "?Ticker=" + Ticker.value).then(function(response){
            response.text().then(function(data){
                    document.getElementById("FetchRequestOutput").innerHTML = data
            })

    })


}

在这里,我定义了希望附加到按钮上的函数(我需要将其包含在AddButton.js和html文件中)

AddButton.js  (importing functions)
import { SendFetchRequest } from '/static/js/SendRequest.js';

function AddForm()
{
    var myform = document.createElement("FORM");
    myform.setAttribute("id", Math.random());
    myform.setAttribute("type", "form");
    myform.action = "javascript: SendFetchRequest()";

    myinput = document.createElement("INPUT");
    myinput.setAttribute("type", "text");
    myinput.setAttribute("value", "SomeDefaultValue");

    mysubmit = document.createElement("INPUT");
    mysubmit,setAttribute("type", "submit");
    mysubmit.setAttribute("value", "Submit")

    myform.appendChild(myinput)
    myform.appendChild(mysubmit)

    document.getElementById("newFormHere").appendChild(myform)
};

这里是动态添加新按钮的代码。

我正在使用flask + Firefox 64测试此代码。 我收到的错误是以下错误。

SyntaxError: import declarations may only appear at top level of a module AddButton.js:6 
SyntaxError: export declarations may only appear at top level of a module  SendRequest.js:6.js:6 

我已遵循此页面上的第一个示例 How do I include a JavaScript file in another JavaScript file?

如果尝试使用“模块”

Loading failed for the module with source “http://127.0.0.1:5000/static/js/AddButton.mjs”.
Loading failed for the module with source “http://127.0.0.1:5000/static/js/SendRequest.mjs”.

客户端甚至无法加载脚本。

1 个答案:

答案 0 :(得分:0)

JarOutputStream jos = ... ClassWriter cw = new ClassWriter(); cw.accept(myClassNode); byte[] bytes = cw.toByteArray(); 应该是type="text/javascript"

请参见this thread