组件名应该始终是多个单词的,根组件 App
以及 <transition>
、<component>
之类的 Vue 内置组件除外。vue
这样作能够避免跟现有的以及将来的 HTML 元素相冲突,由于全部的 HTML 元素名称都是单个单词的。git
组件的 data
必须是一个函数。github
当在组件中使用 data
属性的时候 (除了 new Vue
外的任何地方),它的值必须是返回一个对象的函数。web
Prop 定义应该尽可能详细。ide
在你提交的代码中,prop 的定义应该尽可能详细,至少须要指定其类型。函数
细致的 prop 定义有两个好处:ui
更好的例子spa
// 更好的作法! 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
),让其返回过滤后的列表。code
为了不渲染本应该被隐藏的列表 (好比 v-for="user in users" v-if="shouldShowUsers"
)。这种情形下,请将 v-if
移动至容器元素上 (好比 ul
, ol
)。component
<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>