vue.js - "Parsing error: Unexpected token < ..."使用 VueCli3 和 airbnb eslint

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

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

  • https://github.com/prettier/prettier/issues/3720 (我已经安装了 eslint-plugin-vue)
  • Vue.js eslint Parsing error .: unexpected token (与我的问题并不真正相关,但我还是尝试了)
  • https://github.com/vuejs/eslint-plugin-vue/issues/186
  • "Unexpected token <" for VueJS running with Webpack

  • 这是我的 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>
    

    我不确定我错过了什么。
    感谢您的时间和帮助!

    最佳答案

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

    这是我的 .eslintrc

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

    vue-eslint-parser doc

    关于vue.js - "Parsing error: Unexpected token < ..."使用 VueCli3 和 airbnb eslint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53601579/

    相关文章:

    javascript - 如何将Vue代码放入vue中的<code>标签中

    typescript - Vue CLI 3 typescript - 类型 'x' 上不存在属性 'Vue'

    javascript - Vue.js:无法为动态创建的 HTML 元素编译 SASS 样式

    javascript - Vue - 从 REST API 实时获取数据

    vue.js - 是否可以在 Vue 中使 Prop 有条件,例如prop2 取决于 prop1 的值?

    javascript - 如何解决 Eslint - Module.createRequire 不是函数错误?

    internet-explorer-11 - 支持 IE11 的 vue cli3 库

    unit-testing - 测试异步返回的数据

    vue.js - 在 vue3 项目中安装 element ui 插件的问题

    javascript - TSLint ESLint - 当函数作为属性访问时发出警告?