vue eslint-standard方式的配置


良好的代碼習慣,會讓我們進步快,這就要我們有更好的規范和好的代碼格式。不論我們是入門還是精通,好的代碼規格和格式,可以讓別人在閱讀你的代碼的時候,感覺很清新,一目了然。如果你想讓你的代碼更規范,eslint驗證規則就是我們最好的選擇。eslint規范可以讓我們的代碼更規范,效率更高效.

下面就跟着我一步一步讓我們超神:

  1. 首先在vscode安裝eslint插件和vetur插件

  2. 在文件的根目錄添加 .eslintrc.js

module.exports = {
    root: true,
    env: {
        node: true
    },
    'extends': [
        'plugin:vue/strongly-recommended',
        '@vue/standard'
    ],
    parserOptions: {
        parser: 'babel-eslint'
    },
    rules: {

        // ? javascript rules
        // 開發模式允許使用console
        'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
        // 開發環境允許使用調試 (生產模式禁用)
        'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
        // 允許使用 async-await
        'generator-star-spacing': 'off',
        // 禁止使用 var
        'no-var': 'error',
        // 函數名括號前不需要有空格
        'space-before-function-paren': 'off',
        // 代碼塊中避免多余留白
        'padded-blocks': 'off',
        // 最多出現3個連續空行
        'no-multiple-empty-lines': ['error', {
            'max': 3,
            'maxBOF': 1
        }],

        // 自定義規則
        "no-eval": 0,
        "eqeqeq": 0,
        "no-unused-vars": [
            "error",
            {
                "argsIgnorePattern": "commit"
            }
        ],
        // 自定義規則

        // ? vue rules
        // html屬性必須換行
        'vue/max-attributes-per-line': 'off',
        // 沒有內容的元素需要使用閉合標簽
        'vue/html-self-closing': 'off'
    }
}
  1. 然后設置vscode設置setting.json
ctrl + p  

搜索 setting.json

把我的json貼出來,方便大家設置:

{
    "extensions.ignoreRecommendations": false,
    "files.associations": {
        "*.vue": "vue",
        "*.wxml": "html",
        "*.wxss": "css",
        "*.cjson": "jsonc",
        "*.wxs": "javascript",
        "*.js": "javascript",
        "*.plist": "json"
    },
    "editor.fontFamily": "'JetBrains Mono', Consolas, 'Courier New', monospace",
    "editor.fontLigatures": true, //連字符
    "files.insertFinalNewline": false, //啟用后,保存文件時在文件末尾插入一個最終新行
    "git.autofetch": true,
    "editor.fontSize": 16, //以像素為單位控制字號
    // "editor.fontFamily": "monospace", //控制字體系列
    "git.enableSmartCommit": true, //在沒有暫存的更改時提交所有更改
    "explorer.confirmDelete": true, //控制資源管理器是否應在刪除文件到廢紙簍時進行確認
    "editor.wordWrap": "on", //控制折行方式  off-禁用折行  on-根據視區寬度折行
    //打開新頁面  welcomePage-打開默認頁面  none-不打開
    "workbench.startupEditor": "newUntitledFile",
    //控制在資源管理器內拖放移動文件或文件夾時是否進行確認
    "explorer.confirmDragAndDrop": false,
    "window.zoomLevel": 0, //調整窗口的縮放級別。原始大小是 0
    "git.confirmSync": false,
    "vetur.format.defaultFormatterOptions": {
        "js-beautify-html": {
            // "wrap_attributes": "force-expand-multiline" //強制展開多行
            // "wrap_attributes": "force-aligned"
            "wrap_attributes": "auto"
        },
        "prettyhtml": {
            "printWidth": 100,
            "singleQuote": true,
            "wrapAttributes": false,
            "sortAttributes": true,
        },
        "prettier": {
            "semi": false, // 去掉自動分號
        }
    },
    "beautify.config": {
        "brace_style": "collapse,preserve-inline" //解決花括號中換行
    },
    "vetur.format.defaultFormatter.js": "vscode-typescript", //格式化js代碼
    "vetur.format.defaultFormatter.html": "js-beautify-html", //格式化html代碼
    "editor.formatOnSave": true,
    "vetur.format.options.tabSize": 2,
    "workbench.iconTheme": "vscode-icons",
    // "vetur.format.options.useTabs": true, //是否在每一行的末尾添加分號
    "editor.tabSize": 2,
    //eslint 2.0 以上配置
    "editor.codeActionsOnSave": {
        "source.fixAll.eslint": true
    },
    "[javascript]": {
        "editor.defaultFormatter": "dbaeumer.vscode-eslint"
    },
    "emmet.includeLanguages": {
        "wxml": "html"
    },
    "minapp-vscode.disableAutoConfig": true,
    "tabnine.experimentalAutoImports": true
}

如果有用不到的設置直接刪掉就行,這樣就可以完美的使用eslint,是不是很簡單。


免責聲明!

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



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