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 @ 2023-03-17 15:14  Ratooner  阅读(3294)  评论(0编辑  收藏  举报