我有一个有几千行长的JavaScript const。当我在index.html文件的标记中直接包含这个常量(它是一个对象数组)时,就可以在我正在使用的图表库中使用这个常量。但是,如果我尝试将常量导入到图表库脚本标记中,它将不可用。
如何成功地将JS const导入到html文件中,以便在脚本标记中使用它。
我拥有的html文件中的脚本标记设置为:
<script>
import { extremelyLongJSConst } from "./example.js";
other js content
</script>example.js JS文件使用以下命令导出const:
export const extremelyLongJSConst = {
lots of info
}我曾尝试将脚本标记定义为模块“type=”,但没有成功,甚至不确定在此上下文中是否可能实现我想要做的事情。
发布于 2019-08-24 12:03:53
在您的HTML中:
<script type="module">
import longJSON from './longJSON.js';
console.log(longJSON);
</scriptThe longJSON.js
export default {
some: 'super fancy content'
};有关模块的信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
致以最诚意的问候
https://stackoverflow.com/questions/57634946
复制相似问题