当前位置:首页 > VUE

vue代码规范实现

2026-03-09 00:49:44VUE

Vue 代码规范实现

Vue 项目的代码规范可通过工具链和团队约定实现,以下为常见实践方案:

基础工具配置

安装 ESLint 和 Prettier 作为代码质量和格式的基础工具:

vue代码规范实现

npm install eslint prettier eslint-plugin-vue --save-dev

配置 .eslintrc.js 示例:

vue代码规范实现

module.exports = {
  root: true,
  env: {
    node: true,
  },
  extends: [
    'plugin:vue/vue3-essential',
    'eslint:recommended',
    '@vue/prettier'
  ],
  rules: {
    'vue/multi-word-component-names': 'off'
  }
}

组件命名规范

  • 单文件组件使用 PascalCase 命名(如 MyComponent.vue
  • 基础组件添加 Base 前缀(如 BaseButton.vue
  • 功能性组件使用动词前缀(如 UploadPhoto.vue

模板约定

<template>
  <div class="component-container">
    <ChildComponent 
      :propA="valueA"
      @event="handleEvent"
    />
  </div>
</template>

脚本部分规范

  • 使用 TypeScript 时明确 props 类型
  • 组合式 API 按 refreactivecomputedwatch 顺序组织
  • 避免直接操作 DOM,优先使用 ref
<script setup lang="ts">
const props = defineProps<{
  id: number
  title: string
}>()

const emit = defineEmits(['update'])
</script>

样式处理

  • 作用域样式使用 <style scoped>
  • 深度选择器用 :deep() 替代 /deep/
  • 全局样式单独管理
<style scoped>
.component-container :deep(.el-input) {
  width: 200px;
}
</style>

自动化验证

package.json 中添加校验脚本:

{
  "scripts": {
    "lint": "eslint --ext .js,.vue src",
    "format": "prettier --write src"
  }
}

Git 提交规范

使用 husky 实现提交前校验:

npx husky add .husky/pre-commit "npm run lint"

目录结构建议

src/
├── components/
│   ├── Base/
│   └── Features/
├── composables/
├── views/
├── assets/
└── stores/

标签: 代码vue
分享给朋友:

相关文章

vue实现打字机

vue实现打字机

Vue实现打字机效果 在Vue中实现打字机效果可以通过动态更新文本内容并添加延迟来实现。以下是几种常见的实现方法: 使用setInterval实现 <template> <…

vue实现tablegrid

vue实现tablegrid

Vue 实现 TableGrid 的方法 使用 Element UI 的 Table 组件 Element UI 提供了强大的 Table 组件,可以快速实现表格布局。安装 Element UI 后,…

vue实现granfana

vue实现granfana

Vue 实现类似 Grafana 的仪表盘 要在 Vue 中实现类似 Grafana 的仪表盘功能,需要结合数据可视化库、状态管理和 UI 组件。以下是关键步骤和推荐工具: 数据可视化库 使用 E…

vue实现排序

vue实现排序

Vue 实现排序的方法 在 Vue 中实现排序可以通过多种方式完成,以下是一些常见的方法: 使用计算属性排序数组 计算属性非常适合对数据进行排序,因为它会在依赖的数据变化时自动更新。以下是一个示例:…

vue实现setinterval

vue实现setinterval

在 Vue 中使用 setInterval Vue 中可以通过生命周期钩子和方法结合 setInterval 实现定时任务。以下是一个完整的实现示例: <template> <…

vue 实现注册

vue 实现注册

Vue 实现注册功能 注册功能通常包括表单验证、数据提交和用户反馈。以下是实现注册功能的步骤: 1. 创建注册表单组件 在 Vue 项目中创建一个注册表单组件,包含用户名、邮箱、密码等输入字段。…