欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue項目vscode 安裝eslint插件的方法(代碼自動修復(fù))

 更新時間:2020年04月15日 14:24:29   作者:ranyonsue  
這篇文章主要介紹了Vue項目vscode 安裝eslint插件的方法 代碼自動修復(fù),需要的朋友可以參考下

ESlint:是用來統(tǒng)一JavaScript代碼風格的工具,不包含css、html等。

方法和步驟:

通常情況下vue項目都會添加eslint組件,我們可以查看webpack的配置文件package.json查看,也可以查看工程下是否有.eslintrc.js和.eslintignore查看到eslint是否開啟。

當我們編寫不符合eslint規(guī)范的代碼時,啟動項目會報錯,比如

這個時候可以安裝vscode eslint插件,就可以自動檢測不符合規(guī)范的代碼。打開vscode左側(cè)擴展面板,搜索eslint,點擊安裝,重啟后生效

安裝好之后,還需要在vscode文件中進行設(shè)置:

通過  file->preferences->Settings 出現(xiàn)如下界面:

點擊紅框,則會出現(xiàn)配置文件

把以下代碼復(fù)制到這個文件中:

{
 // vscode默認啟用了根據(jù)文件類型自動設(shè)置tabsize的選項
 "editor.detectIndentation": false,
 // 重新設(shè)定tabsize
 "editor.tabSize": 2,
 // #每次保存的時候自動格式化
 "editor.formatOnSave": true,
 // #每次保存的時候?qū)⒋a按eslint格式進行修復(fù)
 "eslint.autoFixOnSave": true,
 // 添加 vue 支持
 "eslint.validate": [
 "javascript",
 "javascriptreact",
 {
  "language": "vue",
  "autoFix": true
 }
 ],
 // #讓prettier使用eslint的代碼格式進行校驗
 "prettier.eslintIntegration": true,
 // #去掉代碼結(jié)尾的分號
 "prettier.semi": false,
 // #使用帶引號替代雙引號
 "prettier.singleQuote": true,
 // #讓函數(shù)(名)和后面的括號之間加個空格
 "javascript.format.insertSpaceBeforeFunctionParenthesis": true,
 // #讓vue中的js按編輯器自帶的ts格式進行格式化
 "vetur.format.defaultFormatter.js": "vscode-typescript",
 "vetur.format.defaultFormatterOptions": {
 "js-beautify-html": {
  "wrap_attributes": "force-aligned"
  // #vue組件中html代碼格式化樣式
 }
 },
 "window.zoomLevel": 0,
 "explorer.confirmDelete": false,
 "explorer.confirmDragAndDrop": false,
 "editor.renderControlCharacters": true,
 "editor.renderWhitespace": "all"
}

然后在項目的.eslintrc.js中添加如下代碼:

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

module.exports = {
 root: true,
 parserOptions: {
 parser: 'babel-eslint'
 },
 env: {
 browser: true
 },
 extends: [
 // https://github.com/vuejs/eslint-plugin-vue#priority-a-essential-error-prevention
 // consider switching to `plugin:vue/strongly-recommended` or `plugin:vue/recommended` for stricter rules.
 'plugin:vue/essential',
 // https://github.com/standard/standard/blob/master/docs/RULES-en.md
 'standard'
 ],
 // required to lint *.vue files
 plugins: ['vue'],
 // add your custom rules here
 rules: {
 // allow async-await
 'no-console': 'off',
 indent: ['error', 2, { SwitchCase: 1 }],
 semi: ['error', 'always'],
 'space-before-function-paren': [
  'error',
  { anonymous: 'always', named: 'never' }
 ],
 'generator-star-spacing': 'off',
 // allow debugger during development
 'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off'
 }
}

ctrl + s保存代碼后,便會自動修復(fù)格式不正確的代碼

總結(jié)

到此這篇關(guān)于Vue項目vscode 安裝eslint插件的方法(代碼自動修復(fù))的文章就介紹到這了,更多相關(guān)vscode 安裝eslint插件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論