欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue項(xiàng)目代碼格式規(guī)范設(shè)置參考指南

 更新時(shí)間:2022年05月25日 15:11:26   作者:顏醬  
這篇文章主要給大家介紹了關(guān)于vue3簡單封裝input組件和統(tǒng)一表單數(shù)據(jù)的相關(guān)資料,不管你學(xué)習(xí)哪一門編程語言,相信大家都會(huì)略化這一部分,需要的朋友可以參考下

前言

為了盡量統(tǒng)一項(xiàng)目成員的代碼風(fēng)格,降低開發(fā)者對代碼的理解成本,所以我們需要為項(xiàng)目統(tǒng)一代碼格式規(guī)范;同時(shí)我們不能為了降低理解成本,去增加開發(fā)成本,所以我們借助VS Code的相關(guān)插件去完成代碼格式化的功能。

為項(xiàng)目添加eslint

使用vue-cli構(gòu)建的項(xiàng)目,在項(xiàng)目構(gòu)建時(shí),就會(huì)讓你選擇格式化方案,如果是已有項(xiàng)目運(yùn)行vue add eslint添加格式化方式,建議選擇Prettier 的格式化方案;如果是uniapp的項(xiàng)目,如果使用的是vue-cli,也是使用vue add eslint,如果是使用HBuilder構(gòu)建打包,需要安裝另外安裝@vue/cli-service,不然npm run lint無法正常格式化。

添加eslint之后,根據(jù)你的選擇,eslint的配置項(xiàng),可能在.eslintrc.js文件中,如果該文件不存在,配置項(xiàng)應(yīng)該在package.json,默認(rèn)配置應(yīng)該如下:

module.exports = {
  root: true,
  env: {
    node: true,
  },
  extends: [
    "plugin:vue/essential",
    "eslint:recommended",
    "plugin:prettier/recommended",
  ],
  parserOptions: {
    parser: "@babel/eslint-parser",
  }
};

這些配置不建議改動(dòng),如果有其他需求,我們可以在其基礎(chǔ)上進(jìn)行自定義配置。

自定義eslint配置

module.exports = {
  "root": true,
  "env": {
    "node": true
  },
  "extends": [
    "plugin:vue/essential",
    "plugin:vue/recommended",
    "eslint:recommended",
    "@vue/prettier"
  ],
  "parserOptions": {
    "parser": "babel-eslint"
  },
  rules: {
    "no-console": process.env.NODE_ENV === "production" ? "error" : "warn",
    "no-debugger": process.env.NODE_ENV === "production" ? "error" : "warn",
    "no-unused-vars": [
      "error",
      { vars: "all", args: "none", ignoreRestSiblings: true },
    ],
    "vue/order-in-components": ["error", {
      "order": [
        "el",
        "name",
        "key",
        "parent",
        "functional",
        ["delimiters", "comments"],
        ["components", "directives", "filters"],
        "extends",
        "mixins",
        ["provide", "inject"],
        "ROUTER_GUARDS",
        "layout",
        "middleware",
        "validate",
        "scrollToTop",
        "transition",
        "loading",
        "inheritAttrs",
        "model",
        ["props", "propsData"],
        "emits",
        "setup",
        "asyncData",
        "data",
        "fetch",
        "head",
        "computed",
        "watch",
        "watchQuery",
        "LIFECYCLE_HOOKS",
        "onLoad",
        "onShow",
        "onReady",
        "onHide",
        "onUnload",
        "onResize",
        "onPullDownRefresh",
        "onReachBottom",
        "onTabItemTap",
        "onShareAppMessage",
        "onPageScroll",
        "methods",
        ["template", "render"],
        "renderError"
      ]
    }]
  },
  globals: {
    uni: true,
    requirePlugin: true
  },
}

推薦的eslint配置如上。

extends中添加了plugin:vue/recommended,這個(gè)插件是限制了vue的一些代碼規(guī)范,如組件屬性的順序,vue選項(xiàng)的順序等。

rules中的no-console和no-debugger限制了代碼中的console和debugger,在開發(fā)環(huán)境會(huì)生成警告信息,在生產(chǎn)環(huán)境會(huì)提示報(bào)錯(cuò);no-unused-vars對為使用的變量做了限制,除了參數(shù)和reset中不允許出現(xiàn)未使用的變量;vue/order-in-components是在uniapp中對plugin:vue/recommended規(guī)范的一個(gè)補(bǔ)充,uniapp中存在許多vue沒有的選項(xiàng),設(shè)置vue/order-in-components將這些沒有的選項(xiàng)也進(jìn)行格式化排序。

globals中添加使用到的全局變量,如果不添加會(huì)在格式化時(shí)報(bào)錯(cuò)。uni是uniapp常用的全局對象,requirePlugin是微信開發(fā)用的的全局變量。

pre-commit設(shè)置

pre-commit在git commit之前做一些處理,我們需要在提交之前對代碼格式規(guī)范做檢查,避免在項(xiàng)目打包時(shí),出現(xiàn)eslint的報(bào)錯(cuò)。在項(xiàng)目添加lint-staged,然后再package.json中配置(我們使用vue命令添加eslint時(shí),選擇commit時(shí)格式化,會(huì)自動(dòng)幫我們添加):

"gitHooks": {
  "pre-commit": "lint-staged"
},
"lint-staged": {
  "*.{js,jsx,vue}": [
    "vue-cli-service lint --mode production",
    "git add"
  ]
}

VS Code配置

我們需要用的eslintvetur這兩個(gè)插件,eslint插件需要npm全局安裝eslint包,兩個(gè)插件安裝完之后,在VS Code的配置中,設(shè)置:

"[vue]": {
    "editor.defaultFormatter": "octref.vetur"
}

如果無法格式化,可能是格式化工具沖突導(dǎo)致的,設(shè)置:

// 保存時(shí)使用VSCode 自身格式化程序格式化
"editor.formatOnSave": true,
// 保存時(shí)用eslint格式化
"editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
}
// 兩者會(huì)在格式化js時(shí)沖突,所以需要關(guān)閉默認(rèn)js格式化程序 
"javascript.format.enable": false

參考

vue風(fēng)格指南

eslint-plugin-vue

eslint文檔

總結(jié)

到此這篇關(guān)于vue項(xiàng)目代碼格式規(guī)范設(shè)置的文章就介紹到這了,更多相關(guān)vue3封裝input組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論