Uncaught SyntaxError: Cannot use import statement outside a module 处理办法

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- <script src="js/module1.js"></script> -->
<title></title>
</head>
<body>
<script >
import * as m1 from "./module1.js"
//console.log(m1);
</script>
</body>
</html>

测试import module在浏览器出现Uncaught SyntaxError: Cannot use import statement outside a module (at test.html:10:4) 错误提示,网上查资料提示错的原因是用了es6的语法, 浏览器默认将它作为js解析会出现问题,需要将它作为模块导入,script标签默认type="text/javascript",需要改为type="module",更改后正常的:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- <script src="js/module1.js"></script> -->
<title></title>
</head>
<body>
<script type="module">
import * as m1 from "./module1.js"
//console.log(m1);
</script>
</body>
</html>

 

posted @   Ratooner  阅读(3483)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示