Detailed explanation of how to use eslint in vue

Detailed explanation of how to use eslint in vue

1. Description

eslint helps us check whether the code is standardized. This article uses Prettier rules (as well as standardjs , airbnb and other rules). If you want to use the default rules of eslint or define your own rules, you don’t need to download prettier related dependency packages (at the same time, delete the related configuration with 'prettier' in .eslintrc.js. If you don’t delete the configuration and don’t download prettier, an error will be reported when starting the project). I downloaded the eslint plug-in in the vscode environment, which can format non-standard code according to the rules when saving.

2. Download related dependency packages

"eslint"

"babel-eslint"

"eslint-config-prettier"

"eslint-loader"

"eslint-plugin-prettier"

"eslint-plugin-vue"

3. Configuration file .eslintrc.js (there are other ways to configure rules)

Create a new file .eslintrc.js in the project root directory with the following content (you can configure it according to your needs):

module.exports = {
  root: true, // Only apply this rule to the current project parserOptions: {
    "parser": "babel-eslint",
    "sourceType": 'module', // set to "module", "script" (default)
    // "ecmaVersion": 6 // Enable es6 syntax, do not automatically enable es6 global variables},
  parser: "vue-eslint-parser", // To identify .vue files, you need to download eslint-plugin-vue
  // The environment you want to enable env: {
    "browser": true,
    "node": true,
    "es6": true
  },
   //Inherit plugin rules extends: [
    "eslint:recommended", // Start the default rule marked as "√" "plugin:prettier/recommended" // Rules written by others, dependent on package eslint-config-***, here is the prettier rule],
  // Configure the list of plugin names. Plugin names may omit the 'eslint-plugin- ' prefix.
  plugins: [ 
    'vue', // eslint-plugin-vue
    'prettier' // eslint-plugin-prettier
  ],
  // Custom rules have the highest priority "rules": {
    "no-console": 0
  }
}

4. Automatically format code when saving in vscode (version 1.44.0) configuration

Download the eslint plugin for vscode, open the settings.json file, and add:

"editor.codeActionsOnSave": {

"source.fixAll.eslint": true

},

The configuration of other versions may be slightly different. For example, the configuration shown in the figure will take effect on version 1.36.1.

5. Add scripts command in package.json: eslint detects and automatically fixes

"eslint": "eslint --fix --ext .js,.vue --ignore-path .eslintignore ."

eslintignore is the file ignored during configuration detection

6. Close eslint verification for the project

Here we take vue-cli3 and above as an example. Just add the following configuration in vue.config.js

lintOnSave: false

7. eslint Chinese documentation

https://eslint.bootcss.com/docs/user-guide/getting-started

Summarize

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Solution to Eslint code error in vue project
  • Solve the problem of strict mode warning errors in vue eslint development
  • The pitfalls and solutions of eslint encountered in vue-cli project
  • ESLint configuration method in Vue
  • How to turn off eslint detection in vue (multiple methods)
  • How to fix ESLint errors directly with commands in vue project

<<:  In html, set the scroll bar to automatically display when the content in the div exceeds

>>:  Thoughts on copy_{to, from}_user() in the Linux kernel

Recommend

Detailed analysis of MySQL master-slave replication

Preface: In MySQL, the master-slave architecture ...

Some conclusions on developing mobile websites

The mobile version of the website should at least...

HTML CSS3 does not stretch the image display effect

1. Use the transform attribute to display the ima...

Detailed steps for implementing timeout status monitoring in Apache FlinkCEP

CEP - Complex Event Processing. The payment has n...

Media query combined with rem layout in CSS3 to adapt to mobile screens

CSS3 syntax: (1rem = 100px for a 750px design) @m...

React implements double slider cross sliding

This article shares the specific code for React t...

Solve nginx "504 Gateway Time-out" error

Students who make websites often find that some n...

How to modify port 3389 of Windows server 2008 R2 remote desktop

The default port number of the Windows server rem...

Detailed process of installing and configuring MySQL and Navicat prenium

Prerequisite: Mac, zsh installed, mysql downloade...

Detailed explanation of MySQL cumulative calculation implementation method

Table of contents Preface Demand Analysis Mysql u...

A brief discussion on HTML doctype and encoding

DOCTYPE Doctype is used to tell the browser which...

MySQL partitions existing tables in the data table

Table of contents How to operate Operation proces...