最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue3+ts+EsLint+Prettier規(guī)范代碼的方法實(shí)現(xiàn)

 更新時(shí)間:2021年10月25日 15:23:52   作者:開心就是最好  
本文主要介紹在Vue3中使用TypeScript做開發(fā)時(shí),如何安裝與配置EsLint和Prettier,以提高編碼規(guī)范。感興趣的可以了解一下

本文主要介紹在Vue3中使用TypeScript做開發(fā)時(shí),如何安裝與配置EsLint和Prettier,以提高編碼規(guī)范。
(1)EsLint 提供編碼規(guī)范;
(2)Prettier 是一個(gè) Opinionated 的代碼格式化工具。

使用

EsLint的使用

安裝依賴

npm i -D eslint eslint-plugin-vue @typescript-eslint/parser @typescript-eslint/eslint-plugin

這四個(gè)依賴分別是:

  • - `eslint`: EsLint的核心代碼
  • - `eslint-plugin-vue`:[為Vue使用Eslint的插件](https://eslint.vuejs.org/)
  • - `@typescript-eslint/parser`:ESLint的解析器,用于解析typescript,從而檢查和規(guī)范Typescript代碼
  • - `@typescript-eslint/eslint-plugin`:這是一個(gè)ESLint插件,包含了各類定義好的檢測Typescript代碼的規(guī)范

添加配置文件

npx eslint --init

根目錄下增加.eslintrc.js文件。(建議選擇js文件,json不可以寫注釋) 修改配置文件
主要是修改rules中的相關(guān)配置,具體可查看官方配置

/*!
 * https://eslint.bootcss.com/docs/rules/
 * https://eslint.vuejs.org/rules/
 *
 * - 0: off
 * - 1: warn
 * - 2: error
 */
module.exports = {
  root: true,
  env: {
    browser: true,
    node: true,
    es6: true
  },
  parser: 'vue-eslint-parser',
  parserOptions: {
    parser: '@typescript-eslint/parser',
    ecmaVersion: 2020,
    sourceType: 'module',
    jsxPragma: 'React',
    ecmaFeatures: {
      jsx: true
    }
  },
  globals: {
    AMap: false,
    AMapUI: false
  },
  extends: [
    'plugin:vue/vue3-recommended',
    'plugin:@typescript-eslint/recommended',
    'prettier',
    'plugin:prettier/recommended'
  ],
  rules: {
    '@typescript-eslint/ban-ts-ignore': 'off',
    '@typescript-eslint/explicit-function-return-type': 'off',
    '@typescript-eslint/no-explicit-any': 'off',
    '@typescript-eslint/no-var-requires': 'off',
    '@typescript-eslint/no-empty-function': 'off',
    'vue/custom-event-name-casing': 'off',
    'no-use-before-define': 'off',
    '@typescript-eslint/no-use-before-define': 'off',
    '@typescript-eslint/ban-ts-comment': 'off',
    '@typescript-eslint/ban-types': 'off',
    '@typescript-eslint/no-non-null-assertion': 'off',
    '@typescript-eslint/explicit-module-boundary-types': 'off',
    '@typescript-eslint/no-unused-vars': [
      'error',
      {
        argsIgnorePattern: '^_',
        varsIgnorePattern: '^_'
      }
    ],
    'no-unused-vars': [
      'error',
      {
        argsIgnorePattern: '^_',
        varsIgnorePattern: '^_'
      }
    ],
    'space-before-function-paren': 'off',
    'vue/name-property-casing': ['error', 'PascalCase'], // vue/component-definition-name-casing 對組件定義名稱強(qiáng)制使用特定的大小
    'vue/attributes-order': 'off',
    'vue/one-component-per-file': 'off',
    'vue/html-closing-bracket-newline': 'off',
    'vue/max-attributes-per-line': 'off',
    'vue/multiline-html-element-content-newline': 'off',
    'vue/singleline-html-element-content-newline': 'off',
    'vue/attribute-hyphenation': 'off',
    'vue/require-default-prop': 'off',
    'vue/script-setup-uses-vars': 'off',
    'vue/html-self-closing': [
      'error',
      {
        html: {
          void: 'always',
          normal: 'never',
          component: 'always'
        },
        svg: 'always',
        math: 'always'
      }
    ]
  }
}

Prettier的使用

安裝依賴

npm i --save-dev prettier eslint-config-prettier eslint-plugin-prettier

這三個(gè)依賴分別是:

  • - `prettier`:prettier插件的核心代碼
  • - `eslint-config-prettier`:解決ESLint中的樣式規(guī)范和prettier中樣式規(guī)范的沖突,以prettier的樣式規(guī)范為準(zhǔn),使ESLint中的樣式規(guī)范自動失效
  • - `eslint-plugin-prettier`:將prettier作為ESLint規(guī)范來使用

添加配置文件

在項(xiàng)目的根目錄下創(chuàng)建`.prettierrc.js`文件,并添加如下配置

module.exports = {
  printWidth: 120, // 換行字符串閾值
  tabWidth: 2, // 設(shè)置工具每一個(gè)水平縮進(jìn)的空格數(shù)
  useTabs: false,
  semi: false, // 句末是否加分號
  vueIndentScriptAndStyle: true,
  singleQuote: true, // 用單引號
  trailingComma: 'none', // 最后一個(gè)對象元素加逗號
  bracketSpacing: true, // 對象,數(shù)組加空格
  jsxBracketSameLine: true, // jsx > 是否另起一行
  arrowParens: 'always', // (x) => {} 是否要有小括號
  requirePragma: false, // 不需要寫文件開頭的 @prettier
  insertPragma: false // 不需要自動在文件開頭插入 @prettier
}

將Prettier添加到EsLint中

修改`.eslintrc.js`文件,在extends中增加

    'prettier',
    'plugin:prettier/recommended'

其中:

  • - `prettier/@typescript-eslint`:使得@typescript-eslint中的樣式規(guī)范失效,遵循prettier中的樣式規(guī)范
  • - `plugin:prettier/recommended`:使用prettier中的樣式規(guī)范,且如果使得ESLint會檢測prettier的格式問題,同樣將格式問題以error的形式拋出

使用husky和lint-staged構(gòu)建代碼

安裝依賴

npm i --save-dev husky lint-staged

修改package.json
添加以下代碼

    "husky": {
        "hooks": {
            "pre-commit": "lint-staged"
        }
    },
    "lint-staged": {
        "src*/**/*.ts": [
            "prettier --config .prettierrc.js --write",
            "eslint",
            "git add"
        ],
        "src*/**/*.json": [
            "prettier --config .prettierrc.js --write",
            "eslint",
            "git add"
        ]
    }

這樣,在執(zhí)行g(shù)it commit時(shí),EsLint會檢查提交的代碼。

 增加setting.json配置

在.vscode文件夾中增加`setting.json`配置文件,用于自動保存時(shí),自動修復(fù)及檢驗(yàn)代碼。

{
  "typescript.tsdk": "./node_modules/typescript/lib",
  "typescript.enablePromptUseWorkspaceTsdk": true,
  "volar.tsPlugin": true,
  "volar.tsPluginStatus": false,
  //===========================================
  //============= Editor ======================
  //===========================================
  "explorer.openEditors.visible": 0,
  "editor.tabSize": 2,
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "diffEditor.ignoreTrimWhitespace": false,
  //===========================================
  //============= Other =======================
  //===========================================
  "breadcrumbs.enabled": true,
  "open-in-browser.default": "chrome",
  //===========================================
  //============= files =======================
  //===========================================
  "files.eol": "\n",
  "search.exclude": {
    "**/node_modules": true,
    "**/*.log": true,
    "**/*.log*": true,
    "**/bower_components": true,
    "**/dist": true,
    "**/elehukouben": true,
    "**/.git": true,
    "**/.gitignore": true,
    "**/.svn": true,
    "**/.DS_Store": true,
    "**/.idea": true,
    "**/.vscode": false,
    "**/yarn.lock": true,
    "**/tmp": true,
    "out": true,
    "dist": true,
    "node_modules": true,
    "CHANGELOG.md": true,
    "examples": true,
    "res": true,
    "screenshots": true,
    "yarn-error.log": true,
    "**/.yarn": true
  },
  "files.exclude": {
    "**/.cache": true,
    "**/.editorconfig": true,
    "**/.eslintcache": true,
    "**/bower_components": true,
    "**/.idea": true,
    "**/tmp": true,
    "**/.git": true,
    "**/.svn": true,
    "**/.hg": true,
    "**/CVS": true,
    "**/.DS_Store": true
  },
  "files.watcherExclude": {
    "**/.git/objects/**": true,
    "**/.git/subtree-cache/**": true,
    "**/.vscode/**": true,
    "**/node_modules/**": true,
    "**/tmp/**": true,
    "**/bower_components/**": true,
    "**/dist/**": true,
    "**/yarn.lock": true
  },
  "stylelint.enable": true,
  "stylelint.packageManager": "yarn",
  "liveServer.settings.donotShowInfoMsg": true,
  "telemetry.enableCrashReporter": false,
  "workbench.settings.enableNaturalLanguageSearch": false,
  "path-intellisense.mappings": {
    "/@/": "${workspaceRoot}/src"
  },
  "prettier.requireConfig": true,
  "typescript.updateImportsOnFileMove.enabled": "always",
  "workbench.sideBar.location": "left",
  "[javascriptreact]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[typescript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[typescriptreact]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[html]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[css]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[less]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[scss]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[markdown]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  },
  "[vue]": {
    "editor.codeActionsOnSave": {
      "source.fixAll.eslint": false
    }
  },
  "cSpell.words": [
    "vben",
    "windi",
    "browserslist",
    "tailwindcss",
    "esnext",
    "antv",
    "tinymce",
    "qrcode",
    "sider",
    "pinia",
    "sider",
    "nprogress"
  ]
}

參考資料

Prettier官網(wǎng)
EsLint官網(wǎng)
EsLint Rules
Prettier看這一篇就行了
使用EsLint+Prettier規(guī)范TypeScript代碼

到此這篇關(guān)于vue3+ts+EsLint+Prettier規(guī)范代碼的方法實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)vue3 ts 規(guī)范代碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue項(xiàng)目頁面跳轉(zhuǎn)時(shí)瀏覽器窗口上方顯示進(jìn)度條功能

    Vue項(xiàng)目頁面跳轉(zhuǎn)時(shí)瀏覽器窗口上方顯示進(jìn)度條功能

    這篇文章主要介紹了Vue項(xiàng)目頁面跳轉(zhuǎn)時(shí)瀏覽器窗口上方顯示進(jìn)度條功能,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • 解決vue 退出動畫無效的問題

    解決vue 退出動畫無效的問題

    這篇文章主要介紹了解決vue 退出動畫無效的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 詳解Vue ElementUI手動上傳excel文件到服務(wù)器

    詳解Vue ElementUI手動上傳excel文件到服務(wù)器

    這篇文章主要介紹了詳解Vue ElementUI手動上傳excel文件到服務(wù)器,對ElementUI感興趣的同學(xué),可以參考下
    2021-05-05
  • Vue 3 中父子組件雙向綁定的 4 種方式(最新推薦)

    Vue 3 中父子組件雙向綁定的 4 種方式(最新推薦)

    本文介紹了Vue3中實(shí)現(xiàn)父子組件雙向綁定的四種方式并對比了各自適用場景與優(yōu)缺點(diǎn),每種方式結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2025-07-07
  • Vue.use的原理和設(shè)計(jì)源碼探究

    Vue.use的原理和設(shè)計(jì)源碼探究

    這篇文章主要為大家介紹了Vue.use的原理和設(shè)計(jì)源碼探究詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • vue中路由傳參以及跨組件傳參詳解

    vue中路由傳參以及跨組件傳參詳解

    這篇文章主要給大家介紹了關(guān)于vue中路由傳參以及跨組件傳參的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • vue3.0如何在全局掛載對象和方法

    vue3.0如何在全局掛載對象和方法

    這篇文章主要介紹了vue3.0如何在全局掛載對象和方法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • 解決vue select當(dāng)前value沒有更新到vue對象屬性的問題

    解決vue select當(dāng)前value沒有更新到vue對象屬性的問題

    今天小編就為大家分享一篇解決vue select當(dāng)前value沒有更新到vue對象屬性的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • Vue 使用高德地圖添加點(diǎn)標(biāo)記 + 點(diǎn)擊地圖獲取坐標(biāo) + 帶搜索(即地理編碼 + 逆地理編碼)  附完整示例

    Vue 使用高德地圖添加點(diǎn)標(biāo)記 + 點(diǎn)擊地圖獲取坐標(biāo) + 帶搜索(即地

    這篇文章主要介紹了Vue 使用高德地圖添加點(diǎn)標(biāo)記 + 點(diǎn)擊地圖獲取坐標(biāo) + 帶搜索(即地理編碼 + 逆地理編碼)  附完整示例,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2024-01-01
  • webpack+vue.js實(shí)現(xiàn)組件化詳解

    webpack+vue.js實(shí)現(xiàn)組件化詳解

    vue的開發(fā)體驗(yàn)還是比較愉悅的。首先文檔非常友好,所以上手會比較快。其次,配合webpack和vue-loader,每個(gè)頁面都是一個(gè).vue文件,寫起來很方便。所以很適合做組件化開發(fā),這篇文章我們就來一起看看webpack+vue.js如何實(shí)現(xiàn)組件化。
    2016-10-10

最新評論

石城县| 米易县| 西华县| 广灵县| 兴城市| 柳林县| 娱乐| 鄂托克旗| 平南县| 剑阁县| 太保市| 三江| 阳原县| 格尔木市| 马边| 腾冲县| 安龙县| 浦北县| 临邑县| 沁水县| 宁乡县| 将乐县| 清苑县| 雷州市| 云林县| 天门市| 浦江县| 和政县| 新邵县| 临西县| 当阳市| 云林县| 绥棱县| 电白县| 通海县| 苍南县| 田阳县| 若羌县| 勃利县| 无棣县| 册亨县|