【taro react 错误解决】---- Uncaught SyntaxError: Unexpected token ‘<‘

1. 报错提示

Uncaught SyntaxError: Unexpected token '<'

2. 报错截图

在这里插入图片描述

3. 查看报错原因

在这里插入图片描述

4. 分析原因

  1. 这里肯定不可能是错误的,依次向下查找;
  2. head 标签里边的 meta title script 等都是常规,没有多余的 ‘<’,没有报错;
  3. body 标签中 script 的 app.js 的静态资源文件路径错误!
    在这里插入图片描述

5. 造成问题的原因

  1. app.js 是脚手架自动引入,也就是说不是代码的问题,而是配置问题;
  2. 查看 config 下的 index.js 配置;
h5: {
    publicPath: './',
    staticDirectory: 'static',
    postcss: {
      autoprefixer: {
        enable: true,
        config: {
        }
      },
      cssModules: {
        enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
        config: {
          namingPattern: 'module', // 转换模式,取值为 global/module
          generateScopedName: '[name]__[local]___[hash:base64:5]'
        }
      }
    }
  }
  1. publicPath 的值是静态资源路径配置修改为 ‘./’,原来是 ‘/’;
  2. 为什么修改 publicPath 的配置?由于H5打包后去掉路径后的 pages 路径;
  3. 开发时还原 静态资源路径配置;
h5: {
    publicPath: '/',
    staticDirectory: 'static',
    postcss: {
      autoprefixer: {
        enable: true,
        config: {
        }
      },
      cssModules: {
        enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
        config: {
          namingPattern: 'module', // 转换模式,取值为 global/module
          generateScopedName: '[name]__[local]___[hash:base64:5]'
        }
      }
    }
  }
  1. 重新运行 yarn dev:h5 不再报错!

6. 最后报错解决

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_38082783/article/details/120429447