Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法将JavaScript文件错误导入html_Javascript_Html - Fatal编程技术网

无法将JavaScript文件错误导入html

无法将JavaScript文件错误导入html,javascript,html,Javascript,Html,我有index.html文件,其中有html头体标记,还有constant.js文件,我想从中使用常量。如何在constant.js中定义该常量,如何在index.html中使用该常量,以及在何处放置import语句 当我将其包含在HTML的头部部分时 <script type="module" src="../home/constants.js"></script> 我在浏览器中发现了这个错误 Loading module fr

我有index.html文件,其中有html头体标记,还有constant.js文件,我想从中使用常量。如何在constant.js中定义该常量,如何在index.html中使用该常量,以及在何处放置import语句

当我将其包含在HTML的头部部分时

<script type="module" src="../home/constants.js"></script>
我在浏览器中发现了这个错误

Loading module from “http://localhost:8000/constants.js” was blocked because of a disallowed MIME type (“text/html”)
此外,我无法使用sdata_url

Uncaught ReferenceError: sdata_url is not defined
这是我的html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title >CandleCharts</title>

    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"
          integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="https://code.highcharts.com/css/stocktools/gui.css">
    <link rel="stylesheet" type="text/css" href="https://code.highcharts.com/css/annotations/popup.css">

    <script src="https://code.jquery.com/jquery-3.5.1.min.js"
            integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>

    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"
            integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI"
            crossorigin="anonymous"></script>

    <!-- my modules -->

    <script type="module" src="../home/constants.js"></script>
</head>

<body>
<div id="top_bar">


</div>

<div id="container" style="height: 700px; min-width: 310px" class="chart"></div>


<script type="text/javascript">
    console.log(sdata_url)
    

</script>
</body>
</html>

烛台
console.log(sdata_url)

从“./js/constants.js”导入{sdata_url};
console.log(sdata_url);

通过上述方式,我可以使用sdata_url

这是否回答了您的问题?它不工作,你能告诉我如何导入你的URL是错误的,你的服务器是坏的,因为没有返回404错误,或者你的服务器是坏的,并提供了错误的MIME类型的JS。不管怎样,这都是一个服务器问题,您没有向我们显示您的服务器端代码/配置。除此之外:
var
模块中的变量的作用域是模块,因此您无论如何都不能从另一个脚本将其作为全局变量访问。访问它我该怎么做
Uncaught ReferenceError: sdata_url is not defined
<script type="module" >

    import {sdata_url} from "../js/constants.js";

    console.log(sdata_url);
</script>