<html>
<head>
<script type="module" src="topo_space.js"></script>
</head>
<body onload="main()">
</body>
</html>
topo_space.js:
export function main(){
const innerHTML = `<svg id="main_svg" width="959" height="704"></svg>`;
console.log(innerHTML);
document.body.innerHtml = innerHTML;
}
获取错误:
topo_space.html:6 Uncaught ReferenceError: main is not defined at onload
我想坚持使用模块,因为js本身可以进一步导入json作为模块,我觉得这非常酷。
检查此线程以阅读有关此问题的更多信息:
如何使用类型=模块的脚本中的代码
答案是:
window.main = function main() {
const innerHTML = `<svg id="main_svg" width="959" height="704"></svg>`;
console.log(innerHTML);
document.body.innerHtml = innerHTML;
};
<html>
<head>
<script type="module" src="topo_space.js"></script>
</head>
<body onload="main()"></body>
</html>