zoukankan      html  css  js  c++  java
  • Vue(五)Vue规范

    代码规范很重要

    1.组件名应该始终是多个单词的,根组件 App 除外。

    2.组件的 data 必须是一个函数。

    // In a .vue file
    export default {
      data () {
        return {
          foo: 'bar'
        }
      }
    }

    3.Prop 定义应该尽量详细。【包含了类型、校验】

    // 更好的做法!
    props: {
      status: {
        type: String,
        required: true,
        validator: function (value) {
          return [
            'syncing',
            'synced',
            'version-conflict',
            'error'
          ].indexOf(value) !== -1
        }
      }
    }

    类型有:

    单向数据流:所有的 prop 都使得其父子 prop 之间形成了一个单向下行绑定:父级 prop 的更新会向下流动到子组件中,但是反过来则不行。

    4.总是用 key 配合 v-for

    <ul>
      <li
        v-for="todo in todos"
        :key="todo.id"
      >
        {{ todo.text }}
      </li>
    </ul>

    5.永远不要把 v-if 和 v-for 同时用在同一个元素上。

    6.对于应用来说,顶级 App 组件和布局组件中的样式可以是全局的,但是其它所有组件都应该是有作用域的。

    7.在插件、混入等扩展中始终为自定义的私有属性使用 $_ 前缀。并附带一个命名空间以回避和其它作者的冲突 (比如 $_yourPluginName_)。

    var myGreatMixin = {
      // ...
      methods: {
        $_myGreatMixin_update: function () {
          // ...
        }
      }
    }

    8.只要有能够拼接文件的构建系统,就把每个组件单独分成文件

    // 反例
    Vue.component('TodoList', {
      // ...
    })
    
    Vue.component('TodoItem', {
      // ...
    })
    
    // 好例子
    components/
    |- TodoList.js
    |- TodoItem.js
    components/
    |- TodoList.vue
    |- TodoItem.vue

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

    // 反例
    components/
    |- mycomponent.vue
    components/
    |- myComponent.vue
    
    // 好例子
    components/
    |- MyComponent.vue
    components/
    |- my-component.vue

    10.应用特定样式和约定的基础组件 (也就是展示类的、无逻辑的或无状态的组件) 应该全部以一个特定的前缀开头,比如 Base、App 或 V。

    // 反例
    components/
    |- MyButton.vue
    |- VueTable.vue
    |- Icon.vue
    // 好例子
    components/
    |- BaseButton.vue
    |- BaseTable.vue
    |- BaseIcon.vue
    components/
    |- AppButton.vue
    |- AppTable.vue
    |- AppIcon.vue
    components/
    |- VButton.vue
    |- VTable.vue
    |- VIcon.vue

    11.只应该拥有单个活跃实例的组件应该以 The 前缀命名,以示其唯一性。

    // 反例
    components/
    |- Heading.vue
    |- MySidebar.vue
    
    // 好例子
    components/
    |- TheHeading.vue
    |- TheSidebar.vue

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

    13.组件名应该以高级别的 (通常是一般化描述的) 单词开头,以描述性的修饰词结尾

    14.在单文件组件、字符串模板和 JSX 中没有内容的组件应该是自闭合的——在 DOM 模板里永远不要这样做。

    反例
    <!-- 在单文件组件、字符串模板和 JSX 中 -->
    <MyComponent></MyComponent>
    <!-- 在 DOM 模板中 -->
    <my-component/>
    好例子
    <!-- 在单文件组件、字符串模板和 JSX 中 -->
    <MyComponent/>
    <!-- 在 DOM 模板中 -->
    <my-component></my-component>

    15.对于绝大多数项目来说,在单文件组件和字符串模板中组件名应该总是 PascalCase 的——但是在 DOM 模板中总是 kebab-case 的。

    反例
    <!-- 在单文件组件和字符串模板中 -->
    <mycomponent/>
    <!-- 在单文件组件和字符串模板中 -->
    <myComponent/>
    <!-- 在 DOM 模板中 -->
    <MyComponent></MyComponent>
    好例子
    <!-- 在单文件组件和字符串模板中 -->
    <MyComponent/>
    <!-- 在 DOM 模板中 -->
    <my-component></my-component>
    或者
    <!-- 在所有地方 -->
    <my-component></my-component>

    16.尽管在较为简单的应用中只使用 Vue.component 进行全局组件注册时,可以使用 kebab-case 字符串。但是JS/JSX 中的组件名应该始终是 PascalCase 的

    // 反例
    Vue.component('myComponent', {
      // ...
    })
    import myComponent from './MyComponent.vue'
    export default {
      name: 'myComponent',
      // ...
    }
    export default {
      name: 'my-component',
      // ...
    }
    // 好例子
    Vue.component('MyComponent', {
      // ...
    })
    Vue.component('my-component', {
      // ...
    })
    import MyComponent from './MyComponent.vue'
    export default {
      name: 'MyComponent',
      // ...
    }

    17.组件名应该倾向于完整单词而不是缩写。

    18.在声明 prop 的时候,其命名应该始终使用 camelCase,而在模板和 JSX 中应该始终使用 kebab-case

    // 反例
    props: {
      'greeting-text': String
    }
    <WelcomeMessage greetingText="hi"/>
    // 好例子
    props: {
      greetingText: String
    }
    <WelcomeMessage greeting-text="hi"/>

    19.多个属性的元素应该分多行撰写,每个属性一行。

    <!--反例-->
    <img src="https://vuejs.org/images/logo.png" alt="Vue Logo">
    <MyComponent foo="a" bar="b" baz="c"/>
    <!--好例子-->
    <img
      src="https://vuejs.org/images/logo.png"
      alt="Vue Logo"
    >
    <MyComponent
      foo="a"
      bar="b"
      baz="c"
    />

    20.组件模板应该只包含简单的表达式,复杂的表达式则应该重构为计算属性(Computed)或方法(Methods)

    // 反例
    {{
      fullName.split(' ').map(function (word) {
        return word[0].toUpperCase() + word.slice(1)
      }).join(' ')
    }}
    // 好例子
    <!-- 在模板中 -->
    {{ normalizedFullName }}
    // 复杂表达式已经移入一个计算属性
    computed: {
      normalizedFullName: function () {
        return this.fullName.split(' ').map(function (word) {
          return word[0].toUpperCase() + word.slice(1)
        }).join(' ')
      }
    }

    21.应该把复杂计算属性分割为尽可能多的更简单的属性。

    // 反例
    computed: {
      price: function () {
        var basePrice = this.manufactureCost / (1 - this.profitMargin)
        return (
          basePrice -
          basePrice * (this.discountPercent || 0)
        )
      }
    }
    // 好例子
    computed: {
      basePrice: function () {
        return this.manufactureCost / (1 - this.profitMargin)
      },
      discount: function () {
        return this.basePrice * (this.discountPercent || 0)
      },
      finalPrice: function () {
        return this.basePrice - this.discount
      }
    }

    22.非空 HTML 属性值应该始终带引号 (单引号或双引号,选你 JS 里不用的那个)。

    <!--反例-->
    <input type=text>
    <AppSidebar :style={sidebarWidth+'px'}>
    <!--好例子-->
    <input type="text">
    <AppSidebar :style="{  sidebarWidth + 'px' }">

    23.指令缩写 (用 : 表示 v-bind: 和用 @ 表示 v-on:) 应该要么都用要么都不用

    24.单文件组件应该总是让 <script>、<template> 和 <style> 标签的顺序保持一致。且 <style> 要放在最后,因为另外两个标签至少要有一个。

    好例子
    <!-- ComponentA.vue -->
    <script>/* ... */</script>
    <template>...</template>
    <style>/* ... */</style>
    
    <!-- ComponentA.vue -->
    <template>...</template>
    <script>/* ... */</script>
    <style>/* ... */</style>

    25.如果一组 v-if + v-else 的元素类型相同,最好使用 key (比如两个 <div> 元素)。或者让他们两个元素类型不同。

    <!--反例-->
    <div v-if="error">
      错误:{{ error }}
    </div>
    <div v-else>
      {{ results }}
    </div>
    <!--好例子-->
    <div
      v-if="error"
      key="search-status"
    >
      错误:{{ error }}
    </div>
    <div
      v-else
      key="search-results"
    >
      {{ results }}
    </div>
    
    <p v-if="error">
      错误:{{ error }}
    </p>
    <div v-else>
      {{ results }}
    </div>

    26.在 scoped 样式中,类选择器比元素选择器更好,因为大量使用元素选择器是很慢的。

    27.应该优先通过 Vuex 管理全局状态,而不是通过 this.$root 或一个全局事件总线。

    28.自定义事件:始终使用 kebab-case 的事件名

  • 相关阅读:
    TCP—为什么是AIMD?
    虚拟机是怎么实现的?
    漫谈linux文件IO
    关于大型网站技术演进的思考
    大公司里怎样开发和部署前端代码
    spawn-fcgi 代码介绍
    使用python传参form-data格式的txt请求接口
    实战scrapy抓取站长图片数据
    通过requests和lxml模块对网站数据进行爬取
    centos7.5下安装jenkins
  • 原文地址:https://www.cnblogs.com/LUA123/p/10326881.html
Copyright © 2011-2022 走看看