国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

vscode下vue項目中eslint的使用方法

2019-11-19 12:17:51
字體:
供稿:網(wǎng)友

前言

在vscode的vue項目中,關(guān)于代碼檢查和格式化,遇到各種各樣的問題,比如:

  • 不清楚安裝的拓展的功能,導(dǎo)致安裝了重復(fù)功能的拓展
  • 右鍵格式化文檔的時候,不是按eslint的規(guī)則來格式化,導(dǎo)致需要我再次手動調(diào)整
  • 保存時不能自動修復(fù)代碼

以下通過自己的實踐,進行了相應(yīng)配置,目前可以實現(xiàn):

  • 僅安裝2個推薦的拓展
  • 右鍵格式化文檔按照eslint規(guī)則,不會產(chǎn)生錯誤
  • 保存時自動修復(fù)代碼

vscode 拓展安裝

eslint 拓展

該拓展本身不帶任何插件,當前項目要使用該拓展,需要安裝相應(yīng)的npm包(全局安裝或當前項目安裝)

對于 vue 項目,通常在 vscode 中做如下設(shè)置:

 //保存時自動修復(fù)代碼 "eslint.autoFixOnSave": true, "eslint.options": {  // 應(yīng)檢查代碼的文件擴展名數(shù)組  "extensions": [   ".js",   ".vue"  ] }, // 配置要驗證的語言標識和自動修復(fù)選項,比前面兩個配置的結(jié)合更為細粒度話。可以僅配置下面代碼 "eslint.validate": [  "javascript",  "javascriptreact",  "html",  {   "language": "vue",   "autoFix": true  } ],

vetur 拓展

vue 工具,主要有以下功能

Syntax-highlighting 語法高亮

Snippet 快速定義腳手架代碼片段,如:寫script后會跳出export default{xxx},style 后面會帶lang、scope等

Emmet 仿css選擇器快速生成 html/css 代碼

Linting / Error Checking vetur的 Linting 僅用于快速啟動,對于規(guī)則配置需要用eslint.validate

Linting 不可配置,且自帶了一個固定版本的eslint-plugin-vue,一般我們不用。而是采用以下配置:

  • vscode中設(shè)置"vetur.validation.template": false
  • 安裝ESlint拓展,錯誤處理將走eslint
  • 項目中安裝npm i -D eslint eslint-plugin-vue插件
  • 在.eslintrc.*設(shè)置eslint規(guī)則,后面會介紹eslintrc相關(guān)配置

Formatting 即右鍵的Format Document功能,不支持格式化選中內(nèi)容。

可以在設(shè)置中配置vetur.format.defaultFormatter / 如:默認"vetur.format.defaultFormatter.html": "prettyhtml",也可將值設(shè)為 none 就不會格式化該類文件了 / 這個默認設(shè)置非常難用,會將vue文件變得很亂,比如默認加分號,屬性按列展開;我們在設(shè)置中進行如下配置即可實現(xiàn)格式化vue文件時按eslint的規(guī)則來

"vetur.format.defaultFormatterOptions": {"js-beautify-html": {// 屬性列太長才折行,默認的force-expand-multiline不美觀"wrap_attributes": "auto"},"prettier": {//去掉代碼結(jié)尾分號"semi": false,//使用eslint的代碼格式進行校驗"eslintIntegration": true,//采用單引號"singleQuote": true}},//格式化.vue中html,js"vetur.format.defaultFormatter.html": "js-beautify-html","vetur.format.defaultFormatter.js": "vscode-typescript",//讓函數(shù)(名)和后面的括號之間加個空格"javascript.format.insertSpaceBeforeFunctionParenthesis": true,

IntelliSense 智能感知vue文件結(jié)構(gòu),比如<template>中提供了html標簽和屬性的感知,當編輯<template>時如同編輯html文件一樣,讓其他插件可以如html支持一樣進行支持<template>

Debugging 調(diào)試功能

Framework Support for Element UI and Onsen UI UI框架支持

如果想使用Format Selection功能,需要再下載prettier-Code formatter拓展。

但只要配置合理,全文格式化未嘗不可

eslintrc 配置

安裝完上文兩個拓展和進行相應(yīng)配置后,還需要 對.eslintrc.js 進行配置。文件不存在或配置不當,編碼時不會進行錯誤提示

若使用@vue/cli 初始化項目并選擇支持eslint,則默認生成時就存在了。

否則需要手動生成:

詳見Installation

 .eslintrc.js

早期的一個配置

// http://eslint.org/docs/user-guide/configuringmodule.exports = { root: true, parser: 'babel-eslint', parserOptions: { sourceType: 'module' }, env: { browser: true, }, // https://github.com/feross/standard/blob/master/RULES.md#javascript-standard-style extends: 'standard', // required to lint *.vue files plugins: [ 'html' ], // add your custom rules here 'rules': { // allow paren-less arrow functions 'arrow-parens': 0, // allow async-await 'generator-star-spacing': 0, // allow debugger during development 'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0 }}

當前配置(主流):extends配置vue校驗規(guī)則,parser移至parserOptions下,plugins中配置為vue

// http://eslint.org/docs/user-guide/configuringmodule.exports = { root: true, // parser: 'babel-eslint', parserOptions: { sourceType: 'module', parser: 'babel-eslint', }, env: { browser: true, }, // https://github.com/feross/standard/blob/master/RULES.md#javascript-standard-style extends: [ // 按從上往下的規(guī)則匹配 //推薦校驗 "plugin:vue/recommended", //基本校驗 //"plugin:vue/essential", "standard" ], // required to lint *.vue files plugins: [ 'vue' ], // add your custom rules here 'rules': { // allow paren-less arrow functions 'arrow-parens': 0, // allow async-await 'generator-star-spacing': 0, // allow debugger during development 'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0 }}

plugin:vue/recommended 下 wrap_attributes 的規(guī)則是force-expand-multiline

即按上述配置,格式化文檔時,屬性會變成一列(auto),但保存時的eslint 的autoFix會按 force-expand-multiline 多行展開。

覺得麻煩的,可以配置為plugin:vue/essential

配置分享

settings.json

// 將設(shè)置放入此文件中以覆蓋默認設(shè)置{ "editor.fontSize": 12, "editor.tabSize": 2, "files.associations": {  "*.vue": "vue" }, "eslint.autoFixOnSave": true, "eslint.options": {  "extensions": [   ".js",   ".vue"  ] }, "eslint.validate": [  "javascript",  "javascriptreact",  {   "language": "html",   "autoFix": true  },  {   "language": "vue",   "autoFix": true  } ], "vetur.validation.template": false, "vetur.format.defaultFormatterOptions": {  "js-beautify-html": {   // 屬性列太長才折行,默認的force-expand-multiline不美觀   "wrap_attributes": "auto"  },  "prettier": {   //去掉代碼結(jié)尾分號   "semi": false,   //使用eslint的代碼格式進行校驗   "eslintIntegration": true,   //采用單引號   "singleQuote": true  } }, //格式化.vue中html,js "vetur.format.defaultFormatter.html": "js-beautify-html", "vetur.format.defaultFormatter.js": "vscode-typescript", //讓函數(shù)(名)和后面的括號之間加個空格 "javascript.format.insertSpaceBeforeFunctionParenthesis": true, "search.exclude": {  "**/Node_modules": true,  "**/bower_components": true,  "**/dist": true }, "git.confirmSync": false, "window.zoomLevel": 0, "editor.renderWhitespace": "boundary", "editor.cursorBlinking": "smooth", "editor.minimap.enabled": true, "editor.minimap.renderCharacters": false, "editor.fontFamily": "'Droid Sans Mono', 'Courier New', monospace, 'Droid Sans Fallback'", "window.title": "${dirty}${activeEditorMedium}${separator}${rootName}", "editor.codeLens": true, "editor.snippetSuggestions": "top", "workbench.colorTheme": "Solarized Light", "extensions.ignoreRecommendations": false}

參考 :

eslint-plugin-vue: https://eslint.vuejs.org/user-guide/

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對武林網(wǎng)的支持。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宾川县| 新闻| 岗巴县| 上虞市| 汤原县| 高台县| 北海市| 平定县| 海安县| 常宁市| 陕西省| 宁夏| 陇南市| 孝义市| 河源市| 陵川县| 垣曲县| 克什克腾旗| 清远市| 姚安县| 伊川县| 正阳县| 壶关县| 霞浦县| 雅江县| 旅游| 休宁县| 博兴县| 云霄县| 江安县| 卢氏县| 阳高县| 大理市| 利辛县| 洛宁县| 新干县| 嵊州市| 乌兰察布市| 阳新县| 含山县| 天峻县|