【问题标题】:SyntaxError: Unexpected keyword 'const'. Const declaration are not supported in strict modeSyntaxError:意外的关键字“const”。严格模式不支持 const 声明
【发布时间】:2018-11-07 22:34:23
【问题描述】:

Nuxt 1.4 项目中出现上述语法错误。

这发生在 Safari 9 上,同样在 Chrome 上运行良好。

我在SO上搜索,发现问题是由于ES6代码的浏览器兼容性问题,但无法找到问题的适当解决方案(Nuxt项目)。

该网站也应该可以在旧版本的浏览器上运行。

以下是 nuxt.config.js 和 package.json 文件的详细信息

  1. nuxt.config.js
module.exports = {
  /*
  ** Build configuration
  */
  build: {
    /*
    ** Run ESLint on save
    */
    extend (config, { isDev, isClient }) {
      if (isDev && isClient) {
        config.module.rules.push({
          enforce: 'pre',
          test: /\.(js|vue)$/,
          loader: 'eslint-loader',
          exclude: /(node_modules)/
        })
      }
    },
    vendor: ['babel-polyfill']
  }
}
  1. package.json
{
  "dependencies": {
    "axios": "^0.18.0",
    "babel-polyfill": "^6.26.0",
    "nuxt": "^1.4.0",
    "vee-validate": "^2.0.5",
    "vue-carousel": "^0.6.5",
    "vue-i18n": "^7.4.2",
    "vue-smooth-scroll": "^1.0.13",
    "vue-tabs-component": "^1.4.0",
    "vue-youtube-embed": "^2.1.3",
    "vue2-google-maps": "^0.8.8"
  },
  "devDependencies": {
    "ava": "^0.25.0",
    "babel-eslint": "^8.2.1",
    "eslint": "^4.17.0",
    "eslint-config-standard": "^11.0.0-beta.0",
    "eslint-friendly-formatter": "^3.0.0",
    "eslint-loader": "^1.7.1",
    "eslint-plugin-html": "^4.0.2",
    "eslint-plugin-import": "^2.8.0",
    "eslint-plugin-node": "^6.0.0",
    "eslint-plugin-promise": "^3.6.0",
    "eslint-plugin-standard": "^3.0.1",
    "eslint-plugin-vue": "^4.0.0",
    "jsdom": "^11.6.2"
  },
  "babel": {
    "presets": [
      "es2015"
    ]
  }
}

【问题讨论】:

    标签: webpack babeljs nuxt.js


    【解决方案1】:

    您应该使用 Babel 将您的代码编译为常规的 es5 或 es6 代码,具体取决于您正在寻找的兼容性。

    在 Nuxt 中,您可以在 nuxt.config.js 中使用 babel 选项来为您完成编译。 在此处查看babel 选项的文档:https://nuxtjs.org/api/configuration-build#babel

    如果您从 Nuxt 内部收到错误,您应该考虑升级到 nuxt-edge 以获得旧版支持。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-14
      • 2023-04-07
      • 2016-08-15
      • 2014-05-01
      相关资源
      最近更新 更多