1

我正在按照本教程中的步骤进行操作:

https://www.youtube.com/watch?v=z6hQqgvGI4Y

使用 VSCode(版本 1.22.2)作为我的编辑器

我正在运行以下版本

==> vue --version
2.9.3

使用此处概述的步骤从 npm 安装的 Vue / vue-cli :

npm install --global vue-cli

我的 VSCode 工作区设置(用户设置)如下:

{
"workbench.colorTheme": "Visual Studio Dark",
"window.zoomLevel": 1,
"workbench.statusBar.visible": true,
"workbench.startupEditor": "newUntitledFile",
// Format a file on save. A formatter must be available, the file must not be auto-saved, and editor must not be shutting down.
// "editor.formatOnSave": true,
"eslint.autoFixOnSave": true,
// Enable/disable default JavaScript formatter (For Prettier)
"javascript.format.enable": false,
// Use 'prettier-eslint' instead of 'prettier'. Other settings will only be fallbacks in case they could not be inferred from eslint rules.
"prettier.eslintIntegration": false,
"editor.insertSpaces": true,
"[javascript]": {
    "editor.tabSize": 2,
    "editor.insertSpaces": true
},
"[vue]": {
    "editor.tabSize": 2,
    "editor.insertSpaces": true
},
"eslint.options": {
    "extensions": [".html", ".js", ".vue", ".jsx"]
},
"eslint.validate": [
    {
        "language": "html",
        "autoFix": true
    },
    {
        "language": "vue",
        "autoFix": false
    },
    {
        "language": "javascript",
        "autoFix": true
    },
    {
        "language": "javascriptreact",
        "autoFix": true
    }
]

}

我安装了 VSCode 的 Vetur 工具:

https://github.com/vuejs/vetur

我有以下文件:src/components/HomeCentral.vue

<template>
    <div class="homecentral">
        <input type="text" v-model="title"><br/>
        <h1>{{title}}</h1>
        <p v-if="showName">{{user.first_name}}</p>
        <p v-else>Nobody</p>
        <ul>
            <li v-for="item in items" :key="item.id">{{item.title}}</li>
        </ul>
        <button v-on:click="greet('Hello World')">Say Greeting</button>
    </div>
</template>

<script>
export default {
  name: 'HomeCentral',
  data() {
    return {
      title: 'Welcome',
      user: {
        first_name: 'John',
        last_name: 'Doe',
      },
      showName: true,
      items: [
          { title: 'Item One' },
          { title: 'Item Two' },
          { title: 'Item Three' },
      ],
    };
  },
  methods: {
    greet: function (greeting) {
      alert(greeting);
    },
  },
};
</script>

<style scoped>

</style>

src/App.vue

<template>
  <div id="app">
    <home-central></home-central>
  </div>

</template>

<script>
import HomeCentral from './components/HomeCentral';

export default {
  name: 'App',
  components: {
    HomeCentral,
  },
};
</script>
<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

src/路由器/index.js

import Vue from 'vue';
import Router from 'vue-router';
import HomeCentral from '../components/HomeCentral';

Vue.use(Router);

export default new Router({
  routes: [
    {
      path: '/',
      name: 'HomeCentral',
      component: HomeCentral,
    },
  ],
});

我的 .eslintrc 如下所示:

// https://eslint.org/docs/user-guide/configuring

module.exports = {
  root: true,
  parser: 'babel-eslint',
  parserOptions: {
    sourceType: 'module'
  },
  env: {
    browser: true,
    node: true,
    "es6": false
  },
  extends: 'airbnb-base',
  // required to lint *.vue files
  plugins: [
    'html',
    'vue'
  ],
  // check if imports actually resolve
  settings: {
    'import/resolver': {
      webpack: {
        config: 'build/webpack.base.conf.js'
      }
    }
  },
  // add your custom rules here
  rules: {
    // don't require .vue extension when importing
    'import/extensions': ['error', 'always', {
      js: 'never',
      vue: 'never'
    }],
    // disallow reassignment of function parameters
    // disallow parameter object manipulation except for specific exclusions
    'no-param-reassign': ['error', {
      props: true,
      ignorePropertyModificationsFor: [
        'state', // for vuex state
        'acc', // for reduce accumulators
        'e' // for e.returnvalue
      ]
    }],
    // allow optionalDependencies
    'import/no-extraneous-dependencies': ['error', {
      optionalDependencies: ['test/unit/index.js']
    }],
    // allow debugger during development
    'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off'
  }
}

我的 .editorconfig 看起来像这样:

root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

但是当我跑步时

==> npm 运行开发

我得到以下输出:

> webpack-dev-server --inline --progress --config build/webpack.dev.conf.js

 95% emitting                                                                        

 WARNING  Compiled with 1 warnings                                                                                                                                                3:01:35 PM


  ⚠  http://eslint.org/docs/rules/func-names        Unexpected unnamed method 'greet'  
  src/components/HomeCentral.vue:33:12
      greet: function (greeting) {
              ^

  ⚠  http://eslint.org/docs/rules/no-alert          Unexpected alert                   
  src/components/HomeCentral.vue:34:7
        alert(greeting);
         ^

  ✘  http://eslint.org/docs/rules/object-shorthand  Expected method shorthand          
  src/components/HomeCentral.vue:33:5
      greet: function (greeting) {
       ^


✘ 3 problems (1 error, 2 warnings)


Errors:
  1  http://eslint.org/docs/rules/object-shorthand

Warnings:
  1  http://eslint.org/docs/rules/no-alert
  1  http://eslint.org/docs/rules/func-names

You may use special comments to disable some warnings.
Use // eslint-disable-next-line to ignore the next line.
Use /* eslint-disable */ to ignore all warnings in a file.

为什么 ESlint 抱怨“预期方法速记”是错误并指向以下 ES6 linting 规则:

http://eslint.org/docs/rules/object-shorthand

2.9.3 版本的 Vue 使用 ES6 吗?

如何让 VScode 编辑器对这个语义正确的 Vue 代码进行 linting:

在此处输入图像描述

4

2 回答 2

2

通过以下 PeterVojtek 的回答进行修复:

https://github.com/vuejs-templates/webpack/issues/73

基本上更改了 build/webpack.base.conf.js 的以下部分

const createLintingRule = () => ({
  test: /\.(js|vue)$/,
  loader: 'eslint-loader',
  enforce: 'pre',
  include: [resolve('src'), resolve('test')],
  options: {
    formatter: require('eslint-friendly-formatter'),
    emitWarning: !config.dev.showEslintErrorsInOverlay
  }
})

const createLintingRule = () => ({
})

还从 .eslintrc.js 的插件部分中删除了“html”

// https://eslint.org/docs/user-guide/configuring

module.exports = {
  root: true,
  parser: 'babel-eslint',
  parserOptions: {
    sourceType: 'module'
  },
  env: {
    browser: true,
    node: true,
    "es6": false
  },
  extends: [
    'airbnb-base',
  ],
  // required to lint *.vue files
  plugins: [
    'vue',
  ],
  // check if imports actually resolve
  settings: {
    'import/resolver': {
      webpack: {
        config: 'build/webpack.base.conf.js'
      }
    }
  },
  // add your custom rules here
  rules: {
    // don't require .vue extension when importing
    'import/extensions': ['error', 'always', {
      js: 'never',
      vue: 'never'
    }],
    // disallow reassignment of function parameters
    // disallow parameter object manipulation except for specific exclusions
    'no-param-reassign': ['error', {
      props: true,
      ignorePropertyModificationsFor: [
        'state', // for vuex state
        'acc', // for reduce accumulators
        'e' // for e.returnvalue
      ]
    }],
    // allow optionalDependencies
    'import/no-extraneous-dependencies': ['error', {
      optionalDependencies: ['test/unit/index.js']
    }],
    // allow debugger during development
    'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off'
  }
}
于 2018-04-19T20:03:52.687 回答
0

'Angular 10' 中的回调函数中的 TSlint 错误/警告,VS 代码版本 1.53.2 -

tslint 速记错误

解决方案 -

doc.html(htmlData.innerHTML, {
  callback(data: any): void {
    data.save('angular-demo.pdf');
  },
  x: 10,
  y: 10
});

包.json -

{
    "dependencies": {
        "@angular/core": "~10.1.3",
        ...
      },
    "devDependencies": {
        "@angular/cli": "~10.1.3",
        "@types/node": "^12.11.1",
        "ts-node": "~8.3.0",
        "tslint": "~6.1.0",
        "typescript": "~4.0.2",
        ...
    }
}
于 2021-02-16T05:58:27.120 回答