命名风格(推荐)

Component

所有的Component文件都是以大写开头 (PascalCase),这也是官方所推荐的

单文件组件的文件名应该要么始终是单词大写开头 (PascalCase),要么始终是横线连接 (kebab-case)。

但除了 index.vue。

例子:

  1. @/components/BackToTop/index.vue
  2. @/components/Charts/Line.vue
  3. @/views/example/components/Button.vue

JS 文件

命名要明确,简单的直接命名,复杂的.js文件都遵循横线连接 (kebab-case)。

  1. @/utils/filters.js
  2. @/utils/open-window.js
  3. @/views/svg-icons/require-icons.js
  4. @/components/MarkdownEditor/default-options.js

Views

在views文件下,代表路由的.vue文件都使用横线连接 (kebab-case),代表路由的文件夹也是使用同样的规则。

例子:

  1. @/views/svg-icons/index.vue
  2. @/views/svg-icons/require-icons.js

使用横线连接 (kebab-case)来命名views主要是出于以下几个考虑。

  • 横线连接 (kebab-case) 也是官方推荐的命名规范之一 文档
  • views下的.vue文件代表的是一个路由,所以它需要和component进行区分(component 都是大写开头)
  • 页面的url 也都是横线连接的,比如https://www.xxx.admin/export-excel,所以路由对应的view应该要保持统一
  • 没有大小写敏感问题

统一代码规范

保证代码风格统一、报错信息处理

  1. 安装插件:ESlint、Vetur、Beautify
  2. 配置本地vscode的设置 setting.json
  1. {
  2. "vetur.format.defaultFormatter.js": "none",
  3. "vetur.format.defaultFormatter.html": "js-beautify-html",
  4. "vetur.format.defaultFormatterOptions": {
  5. "js-beautify-html": {
  6. "wrap_line_length": 1000,
  7. "wrap_attributes": "auto",
  8. "end_with_newline": false
  9. },
  10. "prettyhtml": {
  11. "printWidth": 600,
  12. "singleQuote": false,
  13. "wrapAttributes": false
  14. }
  15. },
  16. "eslint.format.enable": true,
  17. "eslint.validate": [
  18. "javascript",
  19. "javascriptreact",
  20. {
  21. "language": "vue",
  22. "autoFix": true
  23. }
  24. ],
  25. "editor.formatOnSave": false,
  26. "editor.codeActionsOnSave": {
  27. "source.fixAll.eslint": true
  28. },
  29. "[vue]": {
  30. "editor.defaultFormatter": "octref.vetur"
  31. },
  32. "[javascript]": {
  33. "editor.defaultFormatter": "vscode.typescript-language-features"
  34. }
  35. }

Ts+Vue+tsLint(esLint)

1、安装 Vetur插件
2、settings.json :

  1. {
  2. "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",
  3. "window.zoomLevel": 0,
  4. "editor.fontSize": 15,
  5. "editor.renderWhitespace": "all",
  6. // "vetur.format.options.tabSize": 2,
  7. "vetur.format.defaultFormatter.js": "none",
  8. "vetur.format.defaultFormatter.html": "js-beautify-html",
  9. "vetur.format.defaultFormatterOptions": {
  10. "js-beautify-html": {
  11. "wrap_line_length": 1000,
  12. "wrap_attributes": "auto",
  13. "end_with_newline": false
  14. },
  15. "prettyhtml": {
  16. "printWidth": 600,
  17. "singleQuote": false,
  18. "wrapAttributes": false,
  19. }
  20. },
  21. "eslint.format.enable": true,
  22. "eslint.validate": [
  23. "javascript",
  24. "javascriptreact",
  25. {
  26. "language": "vue",
  27. "autoFix": true
  28. }
  29. ],
  30. "editor.formatOnSave": true,
  31. "editor.codeActionsOnSave": {
  32. "source.fixAll.tsLint": true
  33. },
  34. "editor.tabSize": 2,
  35. "files.associations": {
  36. "*.html": "html"
  37. },
  38. "[vue]": {
  39. "editor.defaultFormatter": "dbaeumer.vscode-eslint"
  40. }
  41. }

.eslintrc.js
3、 项目中 .eslintrc.js

  1. module.exports = {
  2. root: true,
  3. env: {
  4. node: true
  5. },
  6. extends: [
  7. 'plugin:vue/recommended',
  8. '@vue/standard',
  9. '@vue/typescript/recommended'
  10. ],
  11. parserOptions: {
  12. ecmaVersion: 2020
  13. },
  14. rules: {
  15. '@typescript-eslint/ban-types': 'off',
  16. '@typescript-eslint/explicit-module-boundary-types': 'off',
  17. '@typescript-eslint/member-delimiter-style': ['error',
  18. {
  19. multiline: {
  20. delimiter: 'none'
  21. },
  22. singleline: {
  23. delimiter: 'comma'
  24. }
  25. }],
  26. '@typescript-eslint/no-explicit-any': 'off',
  27. 'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
  28. 'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
  29. 'space-before-function-paren': ['error', 'never'],
  30. 'vue/array-bracket-spacing': 'error',
  31. 'vue/arrow-spacing': 'error',
  32. 'vue/block-spacing': 'error',
  33. 'vue/brace-style': 'error',
  34. 'vue/camelcase': 'error',
  35. 'vue/comma-dangle': 'error',
  36. 'vue/component-name-in-template-casing': 'error',
  37. 'vue/eqeqeq': 'error',
  38. 'vue/key-spacing': 'error',
  39. 'vue/match-component-file-name': 'error',
  40. 'vue/object-curly-spacing': 'error',
  41. 'vue/max-attributes-per-line': ['error', {
  42. singleline: 10,
  43. multiline: {
  44. max: 10,
  45. allowFirstLine: true
  46. }
  47. }]
  48. },
  49. overrides: [
  50. {
  51. files: [
  52. '**/__tests__/*.{j,t}s?(x)',
  53. '**/tests/unit/**/*.spec.{j,t}s?(x)'
  54. ],
  55. env: {
  56. jest: true
  57. }
  58. }
  59. ]
  60. }

配合 git Hooks 做 提交前处理

  1. // lint 的检查,如果有未处理的就不让提交上去
  2. "gitHooks": {
  3. "pre-commit": "lint-staged"
  4. },
  5. "lint-staged": {
  6. "*.js": [
  7. "vue-cli-service lint",
  8. "git add"
  9. ],
  10. "*.vue": [
  11. "vue-cli-service lint",
  12. "git add"
  13. ]
  14. },

一套标准的前端代码工作流

https://juejin.cn/post/6921223155621036039#heading-16