使用VueCli3和airbnb eslint的“解析错误:意外的令牌<...”

时间:2018-12-03 20:43:42

标签: vue.js eslint vue-cli-3 airbnb eslint-config-airbnb

我知道这似乎是一个重复的问题,尝试以下操作后,我仍然会遇到这类eslint错误:

这是我package.json设置的一部分:

"devDependencies": {
    "@vue/cli-plugin-babel": "^3.0.5",
    "@vue/cli-plugin-eslint": "^3.0.5",
    "@vue/cli-plugin-unit-jest": "^3.0.5",
    "@vue/cli-service": "^3.0.5",
    "@vue/eslint-config-airbnb": "^4.0.0",
    "@vue/test-utils": "^1.0.0-beta.20",
    "babel-core": "7.0.0-bridge.0",
    "babel-eslint": "^10.0.1",
    "babel-jest": "^23.6.0",
    "eslint": "^5.8.0",
    "eslint-config-airbnb-base": "^13.1.0",
    "eslint-plugin-import": "^2.14.0",
    "eslint-plugin-vue": "^5.0.0-0",
    "vue-template-compiler": "^2.5.17"
  },
  "eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/essential",
      "@vue/airbnb"
    ],
    "rules": {},
    "parserOptions": {
      "parser": "babel-eslint",
      "ecmaVersion": 6
    }
  },

这是我的.eslintrc.js文件:

module.exports = {
  parserOptions: {
    sourceType: 'module',
    allowImportExportEverywhere: true,
  },
  rules: {
    'no-console': 0,
  },
  plugins: ['vue'],
};

这是eslint错误的详细版本:

error: Parsing error: Unexpected token < at src/App.vue:1:1:
> 1 | <template>
    | ^
  2 |   <div id="app">
  3 |     <router-view/>
  4 |   </div>

这是我的App.vue源代码:

<template>
  <div id="app">
    <router-view/>
  </div>
</template>

<script>
export default {
  name: 'BuilderApp',
  data() {
    return {
      dragEvents: ['drag', 'dragstart', 'dragend', 'dragover', 'dragenter', 'dragleave', 'drop'],
    };
  },
  mounted() {
    // prevent default action for all defined dragEvents
    if (this.dragAndDropCapable()) {
      this.dragEvents.forEach((dragEvent) => {
        document.getElementById('app').addEventListener(dragEvent, (e) => {
          e.preventDefault();
          e.stopPropagation();
        }, false);
      });
    }
  },
  methods: {
    dragAndDropCapable() {
      const testDiv = document.createElement('div');
      return (('draggable' in testDiv)
          || ('ondragstart' in testDiv && 'ondrop' in testDiv))
          && 'FormData' in window
          && 'FileReader' in window;
    },
  },
};
</script>

<style>
.filter-disable .filter-count {
  background: #dadada;
}
</style>

我不确定我缺少什么。 谢谢您的时间和帮助!

3 个答案:

答案 0 :(得分:0)

您错过了.eslintrc文件中的“解析器”。

这是我的.eslintrc

{
  "parser": "vue-eslint-parser",
  "parserOptions": {
    "ecmaVersion": 7,
    "sourceType": "module"
  }
}

vue-eslint-parser doc

答案 1 :(得分:0)

我遇到了同样的问题,并通过添加.eslintrc.json文件来解决此问题,例如:

{
    "rules": {
        "no-console": 0,
        "quotes": [2, "double", "avoid-escape"]
    },
    "parser":"vue-eslint-parser",
    "parserOptions": {
        "ecmaVersion": 7,
        "sourceType": "module",
        "ecmaFeatures": {
            "jsx": true,
            "modules": true
        }
    }
}

然后我必须在this之后安装eslint-plugin-vue。

我在herehere中找到了这种解决方案。

我忘了提到在所有更改之后,我也遵循此one

希望这很有帮助,但我不知道。

答案 2 :(得分:0)

将您的methods: {...}更改为methods: function(){...}

我也有类似的问题:

created: {
    url_parts = window.location.href.split("/")
    slug = url_parts[3]
    url = `/api/inventory/product/${slug}`

    axios
      .get(url, slug)
      .then(...)
}

我遇到以下错误:

Failed to compile.

./src/components/StoreInfo.vue
Module Error (from ./node_modules/eslint-loader/index.js):

/Users/alkadelik/Documents/Dev/Django/div/cart_cli/src/components/StoreInfo.vue
  30:4  error  Parsing error: Unexpected token, expected ","

  16 | 
  17 |     url_parts = window.location.href.split("/")
> 18 |     slug = url_parts[3]
     |     ^
  19 |     url = ""

当我转换为:

created: function(){
    const axios = require('axios'); // had to introduce this

    // also notice the introduction of 'let'
    let url_parts = window.location.href.split("/")
    let slug = url_parts[3]
    let url = `/api/inventory/product/${slug}`

    axios
      .get(url, slug)
      .then(...)
}