CSS规范

分号

每个属性声明后面都要加分号。

命名

  1. 尽量不使用id选择器,如需要采用驼峰式命名
  2. 适用有意义的名词命名
  3. 单词全部小写,名词超过1个时,使用-分隔符
  4. scss中的变量、函数、混合、placeholder采用驼峰式命名 ```javascript / class / .element-content { … }

/ id /

myDialog {

  1. ...

}

/ 变量 / $colorBlack: #000;

/ 函数 / @function pxToRem($px) { … }

/ 混合 / @mixin centerBlock { … }

/ placeholder / %myDialog { … }

  1. <a name="zUeTn"></a>
  2. ### 属性声明顺序
  3. 原则:整体到局部,外部到内部,重要属性优先
  4. ```css
  5. .element {
  6. display: block;
  7. float: left;
  8. position: absolute;
  9. top: 0;
  10. right: 0;
  11. bottom: 0;
  12. left: 0;
  13. z-index: 100;
  14. margin: 0 100px;
  15. padding: 50px; // padding习惯写到margin后面
  16. width: 100px;
  17. height: 100px;
  18. border: 1px solid #e5e5e5; border-radius: 3px;
  19. font: normal 13px "Helvetica Neue", sans-serif;
  20. color: #333;
  21. text-align: center;
  22. line-height: 1.5;
  23. background-color: #f5f5f5;
  24. opacity: 1;
  25. }

其他参考规范

腾讯CSS规范指南
百度CSS规范指南
Google CSS规范指南

JS规范

变量

命名方式:小驼峰
命名规范:前缀名词

  1. // bad
  2. let setCount = 10
  3. // good
  4. let maxCount = 10

常量

命名方式:全部大写
命名规范:多个单词时使用分隔符_

  1. // bad
  2. const serverErrorCode = {
  3. success: 200,
  4. repeat: 444
  5. }
  6. // good
  7. const SERVER_ERROR_CODE = {
  8. SUCCESS: 200,
  9. REPEAT: 444
  10. }

函数

命名方式:小驼峰
命名规范:前缀动词

  1. // bad
  2. function wordClass() {}
  3. // good
  4. function saveWordClass() {}
  5. // 常用动词:can、has、is、load、get、set

类

命名方式:大驼峰
命名规范:前缀名词

  1. // bad
  2. class person {}
  3. // good
  4. class Person {}

注释

单行

  1. // 单行注释,注意前面的空格
  2. let maxCount = 123

多行

  1. /**
  2. * 多行注释
  3. * /

Vue规范

文件命名

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

  1. // bad
  2. mycomponent.vue
  3. myComponent.vue
  4. // good
  5. my-component.vue
  6. MyComponent.vue

紧密耦合的组件名

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

  1. // bad
  2. components/
  3. |- TodoList.vue
  4. |- TodoItem.vue
  5. └─ TodoButton.vue
  6. // good
  7. components/
  8. |- TodoList.vue
  9. |- TodoListItem.vue
  10. └─ TodoListItemButton.vue
  11. // good
  12. components/
  13. |- todo-list.vue
  14. |- todo-list-item.vue
  15. └─ todo-list-item-button.vue

自闭合组件

在单文件组件中没有内容的组件应该是自闭合的

  1. <!-- bad -->
  2. <a-input></a-input>
  3. <!-- good -->
  4. <a-input />

props命名

小驼峰命名。内容尽量详细,至少有默认值

  1. // bad
  2. greeting-text: String
  3. // good
  4. greetingText: { type: String, default: ''}