溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Vue項(xiàng)目vscode怎么安裝eslint插件

發(fā)布時(shí)間:2020-08-03 13:47:16 來(lái)源:億速云 閱讀:249 作者:小豬 欄目:web開(kāi)發(fā)

小編這次要給大家分享的是Vue項(xiàng)目vscode怎么安裝eslint插件,文章內(nèi)容豐富,感興趣的小伙伴可以來(lái)了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

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

方法和步驟:

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

Vue項(xiàng)目vscode怎么安裝eslint插件

Vue項(xiàng)目vscode怎么安裝eslint插件

當(dāng)我們編寫(xiě)不符合eslint規(guī)范的代碼時(shí),啟動(dòng)項(xiàng)目會(huì)報(bào)錯(cuò),比如

Vue項(xiàng)目vscode怎么安裝eslint插件

這個(gè)時(shí)候可以安裝vscode eslint插件,就可以自動(dòng)檢測(cè)不符合規(guī)范的代碼。打開(kāi)vscode左側(cè)擴(kuò)展面板,搜索eslint,點(diǎn)擊安裝,重啟后生效

Vue項(xiàng)目vscode怎么安裝eslint插件

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

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

Vue項(xiàng)目vscode怎么安裝eslint插件

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

Vue項(xiàng)目vscode怎么安裝eslint插件

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

{
 // vscode默認(rèn)啟用了根據(jù)文件類(lèi)型自動(dòng)設(shè)置tabsize的選項(xiàng)
 "editor.detectIndentation": false,
 // 重新設(shè)定tabsize
 "editor.tabSize": 2,
 // #每次保存的時(shí)候自動(dòng)格式化
 "editor.formatOnSave": true,
 // #每次保存的時(shí)候?qū)⒋a按eslint格式進(jìn)行修復(fù)
 "eslint.autoFixOnSave": true,
 // 添加 vue 支持
 "eslint.validate": [
 "javascript",
 "javascriptreact",
 {
  "language": "vue",
  "autoFix": true
 }
 ],
 // #讓prettier使用eslint的代碼格式進(jìn)行校驗(yàn)
 "prettier.eslintIntegration": true,
 // #去掉代碼結(jié)尾的分號(hào)
 "prettier.semi": false,
 // #使用帶引號(hào)替代雙引號(hào)
 "prettier.singleQuote": true,
 // #讓函數(shù)(名)和后面的括號(hào)之間加個(gè)空格
 "javascript.format.insertSpaceBeforeFunctionParenthesis": true,
 // #讓vue中的js按編輯器自帶的ts格式進(jìn)行格式化
 "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"
}

然后在項(xiàng)目的.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保存代碼后,便會(huì)自動(dòng)修復(fù)格式不正確的代碼

看完這篇關(guān)于Vue項(xiàng)目vscode怎么安裝eslint插件的文章,如果覺(jué)得文章內(nèi)容寫(xiě)得不錯(cuò)的話,可以把它分享出去給更多人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI