如题所示,在我们学习es6的时候,使用export导出模块,使用import导入模块的语法时,我们需要将我们在html中声明的标签<script></script>类型type指定为module,简单代码如下:

     a.js

     html页面报错Uncaught SyntaxError: Cannot use import statement outside a module

    index.html

    html页面报错Uncaught SyntaxError: Cannot use import statement outside a module

    在浏览器中打开页面,查看控制台,会报如题所示的错误:

    html页面报错Uncaught SyntaxError: Cannot use import statement outside a module 

    报错的原因是用了es6的语法, 浏览器默认将它作为js解析会出现问题,需要将它作为模块导入,因此,我们需要将<script type="text/javascript"></script>修改为<script type="module"></script>,修改之后就会出现正确的结果:

    html页面报错Uncaught SyntaxError: Cannot use import statement outside a module

相关文章:

  • 2022-12-23
  • 2021-10-31
  • 2021-09-25
  • 2022-12-23
  • 2022-12-23
  • 2021-05-22
  • 2022-01-15
  • 2022-12-23
猜你喜欢
  • 2021-11-09
  • 2022-12-23
  • 2021-11-27
  • 2022-12-23
  • 2021-10-16
  • 2023-02-16
  • 2022-12-23
相关资源
相似解决方案