Skip to content
On this page

Vue 项目规范

Vue 编码基础

  1. 组件规范

1.1 组件名为多个单词。

组件名应该始终是多个单词组成(大于等于 2),且命名规范为KebabCase格式。

这样做可以避免跟现有的以及未来的 HTML 元素相冲突,因为所有的 HTML 元素名称都是单个单词的。

正例:

js
export default {
  name: 'TodoItem'
  // ...
}

反例:

js
export default {
  name: 'Todo',
  // ...
}
export default {
  name: 'todo-item',
  // ...
}

1.2 组件/路径/文件名格式

正例:

md
my-component/
|- src
| |- MyComponent.vue

反例:

md
components/
|- my-component.vue
|- My-component.vue

1.3 和父组件紧密耦合的子组件应该以父组件名作为前缀命名

正例:

md
......
|- TodoList.vue
|- TodoListItem.vue
|- TodoListItemButton.vue
|- UserProfileOptions.vue (完整单词)

反例:

md
|- TodoList.vue
|- TodoItem.vue
|- TodoButton.vue
|- UProfOpts.vue (使用了缩写)

1.4 在 Template 模版中使用组件,应使用 PascalCase 模式,并且使用自闭合组件。

正例:

jsx
<!-- 在单文件组件、字符串模板和 JSX 中 -->
<MyComponent />
<Row><table :column="data"/></Row>

反例:

jsx
<my-component /> <row><table :column="data"/></row>

1.5 组件的 data 必须是一个函数

当在组件中使用 data 属性的时候(除 new Vue 以外的任何地方),它的值必须是返回一个对象的函数。因为如果直接是一个对象的话,子组件之间的属性值会互相影响。

正例:

js
export default {
  data () {
    return {
      name: 'jack'
    }
  }
}

反例:

js
export default {
  data: {
    name: 'jack'
  }
}

1.6 Prop 定义应该尽量详细

  • 必须使用 camelCase 驼峰命名
  • 必须指定类型
  • 必须加上注释,表明其含义
  • 必须加上 required 或者 default,两者二选其一
  • 如果有业务需要,必须加上 validator 验证

正例:

js
{
  props: {
    // 组件状态,用于控制组件的颜色
    status: {
      type: String,
      required: true,
      validator: function (value) {
        return [
          'succ',
          'info',
          'error'
        ].indexOf(value) !== -1
      }
    },
      // 用户级别,用于显示皇冠个数
    userLevel:{
      type: String,
      required: true
    }
  }
}

1.7 为组件样式设置作用域

正例:

vue
<template>
  <button class="btn btn-close">X</button>
</template>
<!-- 使用 `scoped` 特性 -->
<style scoped>
.btn-close {
  background-color: red;
}
</style>

反例:

vue
<template>
  <button class="btn btn-close">X</button>
</template>
<!-- 没有使用 `scoped` 特性 -->
<style>
.btn-close {
  background-color: red;
}
</style>

1.8 如果特性元素较多,应该主动换行。

正例:

jsx
<MyComponent foo="a" bar="b" baz="c"
  foo="a" bar="b" baz="c"
  foo="a" bar="b" baz="c"
/>

反例:

jsx
<MyComponent foo="a" bar="b" baz="c" foo="a" bar="b" baz="c" foo="a" bar="b" baz="c" foo="a" bar="b" baz="c"/>
  1. 模板中使用简单的表达式

组件模板应该只包含简单的表达式,复杂的表达式则应该重构为计算属性或方法。复杂表达式会让你的模板变得不那么声明式。我们应该尽量描述应该出现的是什么,而非如何计算那个值。而且计算属性和方法使得代码可以重用。

正例:

vue
<template>
  <p>{{ normalizedFullName }}</p>
</template>
<script>
export default {
  // 复杂表达式已经移入一个计算属性
  computed: {
    normalizedFullName: function () {
      return this.fullName.split(' ').map(function (word) {
        return word[0].toUpperCase() + word.slice(1)
      }).join(' ')
    }
  }
}
</script>

反例:

vue
<template>
  <p>
    {{
      fullName.split(' ').map(function (word) {
          return word[0].toUpperCase() + word.slice(1)
        }).join(' ')
    }}
  </p>
</template>
  1. 指令都使用缩写形式

指令推荐都使用缩写形式,(用 : 表示 v-bind: 、用 @ 表示 v-on: 和用 # 表示 v-slot:)

正例:

jsx
<input
  @input="onInput"
  @focus="onFocus"
/>

反例:

jsx
<input
  v-on:input="onInput"
  @focus="onFocus"
/>
  1. 标签顺序保持一致

单文件组件应该总是让标签顺序保持为

正例:

vue
<template>...</template>
<script>...</script>
<style></style>

反例:

vue
<template>...</template>
<style></style>
<script>...</script>
  1. 必须为 v-for 设置键值 key

  2. v-show 与 v-if 选择

如果运行时,需要非常频繁地切换,使用 v-show ;如果在运行时,条件很少改变,使用 v-if。

  1. script 标签内部结构顺序

components > props > data > computed > watch > filter > 钩子函数(钩子函数按其执行顺序) > methods

  1. Vue Router 规范

8.1 页面跳转数据传递使用路由参数

页面跳转,例如 A 页面跳转到 B 页面,需要将 A 页面的数据传递到 B 页面,推荐使用 路由参数进行传参,而不是将需要传递的数据保存 vuex,然后在 B 页面取出 vuex 的数据,因为如果在 B 页面刷新会导致 vuex 数据丢失,导致 B 页面无法正常显示数据。

正例:

js
let id = ' 123'
this.$router.push({ name: 'userCenter', query: { id: id } })

8.2 使用路由懒加载(延迟加载)机制

js
{
  path: '/uploadAttachment',
  name: 'uploadAttachment',
  meta: {
    title: '上传附件'
  },
  component: () => import('@/view/components/uploadAttachment/index.vue')
},

8.3 router 中的命名规范

path、childrenPoints 命名规范采用kebab-case命名规范(尽量vue文件的目录结构保持一致,因为目录、文件名都是kebab-case,这样很方便找到对应的文件)

name 命名规范采用KebabCase命名规范且和component组件名保持一致!(因为要保持keep-alive特性,keep-alive按照component的name进行缓存,所以两者必须高度保持一致)

js
// 动态加载
export const reload = [
  {
    path: '/reload',
    name: 'reload',
    component: Main,
    meta: {
      title: '动态加载',
      icon: 'icon iconfont'
    },
    children: [
      {
        path: '/reload/smart-reload-list',
        name: 'SmartReloadList',
        meta: {
          title: 'SmartReload',
          childrenPoints: [
            {
              title: '查询',
              name: 'smart-reload-search'
            },
            {
              title: '执行reload',
              name: 'smart-reload-update'
            },
            {
              title: '查看执行结果',
              name: 'smart-reload-result'
            }
          ]
        },
        component: () =>
          import('@/views/reload/smart-reload/smart-reload-list.vue')
      }
    ]
  }
];

8.4 router 中的 path 命名规范

path除了采用kebab-case命名规范以外,必须以 / 开头,即使是children里的path也要以 / 开头。如下示例

目的:

经常有这样的场景:某个页面有问题,要立刻找到这个vue文件,如果不用以/开头,path为parent和children组成的,可能经常需要在router文件里搜索多次才能找到,而如果以/开头,则能立刻搜索到对应的组件

js
{
  path: '/file',
  name: 'File',
  component: Main,
  meta: {
    title: '文件服务',
    icon: 'ios-cloud-upload'
  },
  children: [
    {
      path: '/file/file-list',
      name: 'FileList',
      component: () => import('@/views/file/file-list.vue')
    },
    {
      path: '/file/file-add',
      name: 'FileAdd',
      component: () => import('@/views/file/file-add.vue')
    },
    {
      path: '/file/file-update',
      name: 'FileUpdate',
      component: () => import('@/views/file/file-update.vue')
    }
  ]
}

Vue 项目目录规范

目录说明:

md
.
├── build # 打包脚本相关
│   ├── config # 配置文件
│   ├── generate # 生成器
│   ├── script # 脚本
│   └── vite # vite配置
├── mock # mock文件夹
├── public # 公共静态资源目录
├── src # 主目录
│   ├── api # 接口文件
│   ├── assets # 资源文件
│   │   ├── icons # icon sprite 图标文件夹
│   │   ├── images # 项目存放图片的文件夹
│   │   └── svg # 项目存放svg图片的文件夹
│   ├── components # 公共组件
│   ├── design # 样式文件
│   ├── directives # 指令
│   ├── enums # 枚举/常量
│   ├── hooks # hook
│   │   ├── component # 组件相关hook
│   │   ├── core # 基础hook
│   │   ├── event # 事件相关hook
│   │   ├── setting # 配置相关hook
│   │   └── web # web相关hook
│   ├── layouts # 布局文件
│   │   ├── default # 默认布局
│   │   ├── iframe # iframe布局
│   │   └── page # 页面布局
│   ├── locales # 多语言
│   ├── logics # 逻辑
│   ├── main.ts # 主入口
│   ├── router # 路由配置
│   ├── settings # 项目配置
│   │   ├── componentSetting.ts # 组件配置
│   │   ├── designSetting.ts # 样式配置
│   │   ├── encryptionSetting.ts # 加密配置
│   │   ├── localeSetting.ts # 多语言配置
│   │   ├── projectSetting.ts # 项目配置
│   │   └── siteSetting.ts # 站点配置
│   ├── store # 数据仓库
│   ├── utils # 工具类
│   └── views # 页面
├── test # 测试
│   └── server # 测试用到的服务
│       ├── api # 测试服务器
│       ├── upload # 测试上传服务器
│       └── websocket # 测试ws服务器
├── types # 类型文件
├── vite.config.ts # vite配置文件
└── windi.config.ts # windcss配置文件