我有一个长达几千行的 JavaScript const。当我将这个常量(一个对象数组)直接包含在我的 index.html 文件的标签内时,该常量可在我正在使用的图表库中使用。但是,如果我尝试将该 const 导入到图表库的脚本标记中,则它不可用。
如何成功地将 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="module",但没有成功,甚至不确定我想要做的事情在这种情况下是否可行。
在 HTML 中:
<script type="module">
import longJSON from './longJSON.js';
console.log(longJSON);
</script
longJSON.js
export default {
some: 'super fancy content'
};
有关模块的信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules