首页 > 其他分享 >Vue学习笔记4-项目开发规范及插件

Vue学习笔记4-项目开发规范及插件

时间:2022-08-18 10:58:23浏览次数:55  
标签:插件 Vue vue vscode 笔记 typescript eslint true prettier

Vue学习笔记4-项目开发规范及插件

一、安装插件

首先搜索安装 ESLint 和 Prettier 这两个插件。

这里对开发规范的配置仅配置ESLint,对代码格式的配置仅配置Prettier,用于代码格式化。

二、安装依赖

在项目中安装依赖:

npm i -D eslint eslint-plugin-vue eslint-define-config # eslink
npm i -D prettier eslint-plugin-prettier @vue/eslint-config-prettier # prettire
npm i -D @vue/eslint-config-typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser # 对ts的支持

三、配置文件

然后我们依次编写一下对应的配置文件:
2.1. 在项目根目录新建 ESLint 风格检查配置文件:.eslintrc.js

import { defineConfig } from 'eslint-define-config'

export default defineConfig({
  root: true,
  /* 指定如何解析语法。*/
  parser: 'vue-eslint-parser',
  /* 优先级低于parse的语法解析配置 */
  parserOptions: {
    parser: '@typescript-eslint/parser',
    //模块化方案
    sourceType: 'module',
  },
  env: {
    browser: true,
    es6: true,
    node: true,
    // 解决 defineProps and defineEmits generate no-undef warnings
    'vue/setup-compiler-macros': true,
  },
  // https://eslint.bootcss.com/docs/user-guide/configuring#specifying-globals
  globals: {},
  extends: [
    'plugin:vue/vue3-recommended',
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended', // typescript-eslint推荐规则,
    'prettier',
    'plugin:prettier/recommended',
    './.eslintrc-auto-import.json',
  ],
  // https://cn.eslint.org/docs/rules/
  rules: {
    // 禁止使用 var
    'no-var': 'error',
    semi: 'off',
    // 优先使用 interface 而不是 type
    '@typescript-eslint/consistent-type-definitions': ['error', 'interface'],
    '@typescript-eslint/no-explicit-any': 'off', // 可以使用 any 类型
    '@typescript-eslint/explicit-module-boundary-types': 'off',
    // 解决使用 require() Require statement not part of import statement. 的问题
    '@typescript-eslint/no-var-requires': 0,
    // https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/docs/rules/ban-types.md
    '@typescript-eslint/ban-types': [
      'error',
      {
        types: {
          // add a custom message to help explain why not to use it
          Foo: "Don't use Foo because it is unsafe",

          // add a custom message, AND tell the plugin how to fix it
          String: {
            message: 'Use string instead',
            fixWith: 'string',
          },

          '{}': {
            message: 'Use object instead',
            fixWith: 'object',
          },
        },
      },
    ],
    // 禁止出现未使用的变量
    '@typescript-eslint/no-unused-vars': [
      'error',
      { vars: 'all', args: 'after-used', ignoreRestSiblings: false },
    ],
    'prettier/prettier': ['error', { singleQuote: true, parser: 'flow', semi: false }],
    'vue/html-indent': 'off',
    // 关闭此规则 使用 prettier 的格式化规则,
    'vue/max-attributes-per-line': ['off'],
    // 优先使用驼峰,element 组件除外
    'vue/component-name-in-template-casing': [
      'error',
      'PascalCase',
      {
        ignores: ['/^el-/', '/^router-/'],
        registeredComponentsOnly: false,
      },
    ],
    // 强制使用驼峰
    camelcase: ['error', { properties: 'always' }],
    // 优先使用 const
    'prefer-const': [
      'error',
      {
        destructuring: 'any',
        ignoreReadBeforeAssign: false,
      },
    ],
  },
})

2.2. 在项目根目录新建 ESLint 的代码检测忽略的文件的配置文件:.eslintignore

/node_modules/
/public/
.vscode
.idea

2.3. 在项目根目录新建 Prettier 的代码格式化配置文件:.prettierrc

{
  "semi": false,
  "singleQuote": true,
  "printWidth": 100,
  "trailingComma": "all",
  "arrowParens": "avoid",
  "endOfLine": "lf"
}

2.4. 在项目中我们最好是使用统一行尾符(建议不管还是 mac 还是 windows 都使用 lf ),但是按上面的配置,我们发现保存的时候无法将 crlf 行尾符转换成 lf 行尾符,当然我们可以直接点击 vscode 的右下角切换行尾符,但终究是有点麻烦,这时使用 .editorconfig 就很有必要了。在项目根路径新建文件 .editorconfig

root = true

[*]
charset = utf-8
end_of_line = lf

四、编辑器设置

4.1. 首先在项目根目录看有没有 .vscode 文件夹,若没有,就新建。

若想代码保存时自动格式化,新建 settings.json 文件:

{
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  },
  "editor.formatOnSave": true
}

一键安装项目推荐的vscode插件,新建 extensions.json 文件:

{
  "recommendations": [
    "vue.volar",
    "ms-ceintl.vscode-language-pack-zh-hans",
    "mikestead.dotenv",
    "donjayamanne.githistory",
    "lokalise.i18n-ally",
    "esbenp.prettier-vscode",
    "dbaeumer.vscode-eslint",
    "zhucy.project-tree",
    "eamodio.gitlens",
    "ms-vscode.powershell",
    "vscode-icons-team.vscode-icons"
  ]
}

4.2. 提交以上文件到 git 代码仓库,在 .gitignore 文件中配置:

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
!.vscode/settings.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

4.3. 团队其他成员拉代码后, 打开 vscode, 依次点击1,2,3, 会自动输入@recommended, 工作区推荐的插件就是 .vscode/extensions.json 文件推荐的。

image

标签:插件,Vue,vue,vscode,笔记,typescript,eslint,true,prettier
From: https://www.cnblogs.com/zhaifanhua/p/16597922.html

相关文章

  • Vue面试题03:Vue生命周期相关
    生命周期相关定义:每个Vue组件实例在创建时都需要经历一系列的初始化步骤,比如数据监测,模板编译,挂载实例到DOM,以及在数据改变时更新DOM。在此过程中会运行被称为生命......
  • 认识Vue扩展插件
    众所周知,在Vue开发中,实现一个功能可以有很多种方式可以选择,这依赖于Vue强大的功能(指令、混合、过滤等)Vue插件插件通常用来为Vue添加全局功能。插件的功能范围没......
  • vue中改变数组对象属性名名称
    letnape=[];for(leti=0;i<list.length;i++){letres=JSON.parse(JSON.stringify(list[i])......
  • 《伯恩斯焦虑自助疗法》读书笔记3
      每日情绪日志       基于逻辑的语义疗法,包括过程结果法和语义法。  过程结果法:你可以根据你投入的努力过程,来评价自己的结果,过程和结果同样重要。......
  • vue 入门
    idea、webstorm、vsCode,都可以开发吧,脚手架vue-cli项目框架一搭建,就写代码了 --关于vue需要掌握的知识点--- 使用的开发工具是webstorm,它是默认就安装好了vuejs......
  • 部分功能实现笔记
    部分功能实现笔记以下内容均来自武沛齐老师的课程笔记Fields的选择classMyForm(ModelForm):xx=form.CharField*("...")#新加不在数据库中的字段classMe......
  • vue数据双向绑定
    vue数据双向绑定 <divid="app">单向数据绑定:<inputtype="text"v-bind:value="txt"><br><br>双向数据绑定:<inputtype="text"v-model:va......
  • vue学习之------vue-router【命名路由】
    命名路由的概念:就是在定义路由规则时,为当前规则去一个名称,增加name属性。name属性不能重复,必须保证唯一性~ (1)用命名路由实现声明式导航  (2)命名路由实现编程......
  • 02.JavaScript学习笔记1
    JavaScript学习笔记1.强制类型转换当使用加号进行运算时,会将数字强制转换为字符串,然后再进行运算。constyear='1991';console.log(year+18);console.log(typeo......
  • vue学习之------vue-router【编程式导航】
    通过调用API实现导航的方式,叫编程式导航。通过点击链接实现导航的方式,叫声明式导航。 (1)跳转到指定地址的API:this.$router.push('hash地址')(2)实现导航历史的前进、......