zoukankan      html  css  js  c++  java
  • vue 风格指南-必须的

    组件名为多个单词

    组件名应该始终是多个单词的,根组件 App 以及 <transition><component> 之类的 Vue 内置组件除外。

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

    组件数据-data

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

    当在组件中使用 data 属性的时候 (除了 new Vue 外的任何地方),它的值必须是返回一个对象的函数。

    组件数据-prop

    Prop 定义应该尽量详细。

    在你提交的代码中,prop 的定义应该尽量详细,至少需要指定其类型。

    细致的 prop 定义有两个好处:

    • 它们写明了组件的 API,所以很容易看懂组件的用法;
    • 在开发环境下,如果向一个组件提供格式不正确的 prop,Vue 将会告警,以帮助你捕获潜在的错误来源。

    更好的例子

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

    为 v-for 设置键值

    避免 v-if 和 v-for 用在一起

    • 为了过滤一个列表中的项目 (比如 v-for="user in users" v-if="user.isActive")。在这种情形下,请将 users 替换为一个计算属性 (比如 activeUsers),让其返回过滤后的列表。

    • 为了避免渲染本应该被隐藏的列表 (比如 v-for="user in users" v-if="shouldShowUsers")。这种情形下,请将 v-if 移动至容器元素上 (比如 ulol)。

    为组件样式设置作用域

    <template>
      <button class="button button-close">X</button>
    </template>
    
    <!-- 使用 `scoped` 特性 -->
    <style scoped>
    .button {
      border: none;
      border-radius: 2px;
    }
    
    .button-close {
      background-color: red;
    }
    </style>
    
    <template>
      <button :class="[$style.button, $style.buttonClose]">X</button>
    </template>
    
    <!-- 使用 CSS Modules -->
    <style module>
    .button {
      border: none;
      border-radius: 2px;
    }
    
    .buttonClose {
      background-color: red;
    }
    </style>
    
    <template>
      <button class="c-Button c-Button--close">X</button>
    </template>
    
    <!-- 使用 BEM 约定 -->
    <style>
    .c-Button {
      border: none;
      border-radius: 2px;
    }
    
    .c-Button--close {
      background-color: red;
    }
    </style>
  • 相关阅读:
    CodeForces
    处女座的测验(一)(素数筛+思维)
    Codeforces-D-Diverse Garland(思维)
    linux中open函数使用
    linux管道通信
    linux中memset的正确用法
    在linux中read、write函数
    Ubuntu+Win7+Samba实现文件共享
    【转】教你如何实现linux和W…
    《转》我的ARM学习经历
  • 原文地址:https://www.cnblogs.com/liuguiqian/p/11174064.html
Copyright © 2011-2022 走看看