vs工程配置eslint檢測環境


vs工程打開一個js文件,會提示

"No ESLint configuration (e.g .eslintrc) found for file ......."

"Failed to load the ESLint library for the document ......."

就是需要配置eslint檢測環境

  • 首先使用npm 全局安裝eslint
  • 在項目目錄中執行eslint --init,這時候會讓你選擇項目的一些參數,比如模塊類型(JavaScript modules (import/export) CommonJS (require/exports) ),框架(vue或react),執行完畢后生成.eslintrc.js(報的錯不管)
  • 如果選擇vue,還需要安裝eslint-plugin-vue插件,這時需要本地安裝這個插件

  npm i -save-dev eslint-plugin-vue

  • 需要對.eslintrc.js做一些配置,比如之前選擇了JavaScript modules,后面又使用module.exports,必須再加上
"commonjs": true
比如:
module.exports = {
"env": {
"browser": true,
"es6": true,
"commonjs": true
},
........
}
 
  • 如果需要在控制台打印調試信息,需要加上:
"no-console":"off"
比如:
module.exports = {
......
"rules": {
"no-console":"off"
}
};
off也可以用0替代
 "off" -> 0 關閉規則
 "warn" -> 1 開啟警告規則
"error" -> 2 開啟錯誤規則

 

  • 允許使用一些全局變量,比如node里的__dirname
"env": {
......
"node": true
},

 

  • Eslint中no-undef的檢查報錯

有時候使用一些全局變量,會報這個錯誤,

這個可以在eslint中增加一個global配置,用於標記哪些可以使用的全局對象

"globals":{
  "document": true,
  "localStorage": true,
  "window": true
}

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM