命名风格(推荐)
Component
所有的Component文件都是以大写开头 (PascalCase),这也是官方所推荐的
单文件组件的文件名应该要么始终是单词大写开头 (PascalCase),要么始终是横线连接 (kebab-case)。
但除了 index.vue。
例子:
@/components/BackToTop/index.vue@/components/Charts/Line.vue@/views/example/components/Button.vue
JS 文件
命名要明确,简单的直接命名,复杂的.js文件都遵循横线连接 (kebab-case)。
@/utils/filters.js@/utils/open-window.js@/views/svg-icons/require-icons.js@/components/MarkdownEditor/default-options.js
Views
在views文件下,代表路由的.vue文件都使用横线连接 (kebab-case),代表路由的文件夹也是使用同样的规则。
例子:
@/views/svg-icons/index.vue@/views/svg-icons/require-icons.js
使用横线连接 (kebab-case)来命名views主要是出于以下几个考虑。
- 横线连接 (kebab-case) 也是官方推荐的命名规范之一 文档
- views下的.vue文件代表的是一个路由,所以它需要和component进行区分(component 都是大写开头)
- 页面的url 也都是横线连接的,比如https://www.xxx.admin/export-excel,所以路由对应的view应该要保持统一
- 没有大小写敏感问题
统一代码规范
保证代码风格统一、报错信息处理
- 安装插件:ESlint、Vetur、Beautify
- 配置本地vscode的设置 setting.json
{"vetur.format.defaultFormatter.js": "none","vetur.format.defaultFormatter.html": "js-beautify-html","vetur.format.defaultFormatterOptions": {"js-beautify-html": {"wrap_line_length": 1000,"wrap_attributes": "auto","end_with_newline": false},"prettyhtml": {"printWidth": 600,"singleQuote": false,"wrapAttributes": false}},"eslint.format.enable": true,"eslint.validate": ["javascript","javascriptreact",{"language": "vue","autoFix": true}],"editor.formatOnSave": false,"editor.codeActionsOnSave": {"source.fixAll.eslint": true},"[vue]": {"editor.defaultFormatter": "octref.vetur"},"[javascript]": {"editor.defaultFormatter": "vscode.typescript-language-features"}}
Ts+Vue+tsLint(esLint)
1、安装 Vetur插件
2、settings.json :
{"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe","window.zoomLevel": 0,"editor.fontSize": 15,"editor.renderWhitespace": "all",// "vetur.format.options.tabSize": 2,"vetur.format.defaultFormatter.js": "none","vetur.format.defaultFormatter.html": "js-beautify-html","vetur.format.defaultFormatterOptions": {"js-beautify-html": {"wrap_line_length": 1000,"wrap_attributes": "auto","end_with_newline": false},"prettyhtml": {"printWidth": 600,"singleQuote": false,"wrapAttributes": false,}},"eslint.format.enable": true,"eslint.validate": ["javascript","javascriptreact",{"language": "vue","autoFix": true}],"editor.formatOnSave": true,"editor.codeActionsOnSave": {"source.fixAll.tsLint": true},"editor.tabSize": 2,"files.associations": {"*.html": "html"},"[vue]": {"editor.defaultFormatter": "dbaeumer.vscode-eslint"}}
.eslintrc.js
3、 项目中 .eslintrc.js
module.exports = {root: true,env: {node: true},extends: ['plugin:vue/recommended','@vue/standard','@vue/typescript/recommended'],parserOptions: {ecmaVersion: 2020},rules: {'@typescript-eslint/ban-types': 'off','@typescript-eslint/explicit-module-boundary-types': 'off','@typescript-eslint/member-delimiter-style': ['error',{multiline: {delimiter: 'none'},singleline: {delimiter: 'comma'}}],'@typescript-eslint/no-explicit-any': 'off','no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off','no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off','space-before-function-paren': ['error', 'never'],'vue/array-bracket-spacing': 'error','vue/arrow-spacing': 'error','vue/block-spacing': 'error','vue/brace-style': 'error','vue/camelcase': 'error','vue/comma-dangle': 'error','vue/component-name-in-template-casing': 'error','vue/eqeqeq': 'error','vue/key-spacing': 'error','vue/match-component-file-name': 'error','vue/object-curly-spacing': 'error','vue/max-attributes-per-line': ['error', {singleline: 10,multiline: {max: 10,allowFirstLine: true}}]},overrides: [{files: ['**/__tests__/*.{j,t}s?(x)','**/tests/unit/**/*.spec.{j,t}s?(x)'],env: {jest: true}}]}
配合 git Hooks 做 提交前处理
// lint 的检查,如果有未处理的就不让提交上去"gitHooks": {"pre-commit": "lint-staged"},"lint-staged": {"*.js": ["vue-cli-service lint","git add"],"*.vue": ["vue-cli-service lint","git add"]},
