1. 程式人生 > 實用技巧 >使用eslint將專案中的程式碼修改統一的縮排

使用eslint將專案中的程式碼修改統一的縮排

背景

繼承了組裡師兄師姐寫的專案的前端程式碼,但是是兩個人寫的,有兩格縮排的,有四格縮排的,有字串外用單引號的,有用雙引號的。

於是搜尋了一下,可以用eslint強制轉化。

eslint在github上的連結:https://github.com/eslint/eslint

安裝

  1. 用VS Code開啟剛下載好的專案,npm init專案初始化後(一般不需要這個步驟,這是為了載入各種npm包)
    在終端npm install eslint --save-dev
    結果:


    不建議用 npm install eslint -g全域性安裝,因為往下會發現eslint的配置有很多選擇,不同專案使用的配置可能不一樣。

  2. ./node_modules/.bin/eslint --init

    在這個專案上初始化eslint,然後會有一個初始化嚮導。

    上面的問題三種選擇:
    如果選擇 Use a popular style guide 有:

    如果選擇 Answer questions about your style:

    完成初始化嚮導後:
    The config that you've selected requires the following dependencies:

    eslint-plugin-vue@latest
    Successfully created .eslintrc.js file in

  3. 根據提示 npm installeslint-plugin-vue@latest


  4. 在終端敲命令
    ./node_modules/.bin/eslint yourfile.js/yourdir--fix

遇到的問題

1. ESLint is disabled since its execution has not been approved or denied yet. Use the light bulb menu to open the approval dialog.


點選右下角的ESLint,選擇Allow Everywhere即可。

答案來自