Where multiple, equally good options exist, an arbitrary choice can be made to ensure consistency. In these rules, we describe each acceptable option and suggest a default choice. That means you can feel free to make a different choice in your own codebase, as long as you're consistent and have a good reason. Please do have a good reason though! By adapting to the community standard, you will:
Component/instance options should be ordered consistently.
This is the default order we recommend for component options. They're split into categories, so you'll know where to add new properties from plugins.
Global Awareness (requires knowledge beyond the component)
nameTemplate Compiler Options (changes the way templates are compiled)
compilerOptionsTemplate Dependencies (assets used in the template)
componentsdirectivesComposition (merges properties into the options)
extendsmixinsprovide/inject
Interface (the interface to the component)
inheritAttrspropsemitsComposition API (the entry point for using the Composition API)
setupLocal State (local reactive properties)
datacomputedEvents (callbacks triggered by reactive events)
watchbeforeCreatecreatedbeforeMountmountedbeforeUpdateupdatedactivateddeactivatedbeforeUnmountunmountederrorCapturedrenderTrackedrenderTriggeredNon-Reactive Properties (instance properties independent of the reactivity system)
methodsRendering (the declarative description of the component output)
template/render
The attributes of elements (including components) should be ordered consistently.
This is the default order we recommend for component options. They're split into categories, so you'll know where to add custom attributes and directives.
Definition (provides the component options)
isList Rendering (creates multiple variations of the same element)
v-forConditionals (whether the element is rendered/shown)
v-ifv-else-ifv-elsev-showv-cloakRender Modifiers (changes the way the element renders)
v-prev-onceGlobal Awareness (requires knowledge beyond the component)
idUnique Attributes (attributes that require unique values)
refkeyTwo-Way Binding (combining binding and events)
v-modelOther Attributes (all unspecified bound & unbound attributes)
Events (component event listeners)
v-onContent (overrides the content of the element)
v-htmlv-textYou may want to add one empty line between multi-line properties, particularly if the options can no longer fit on your screen without scrolling.
When components begin to feel cramped or difficult to read, adding spaces between multi-line properties can make them easier to skim again. In some editors, such as Vim, formatting options like this can also make them easier to navigate with the keyboard.
defineProps({
value: {
type: String,
required: true
},
focused: {
type: Boolean,
default: false
},
label: String,
icon: String
})
const formattedValue = computed(() => {
// ...
})
const inputClasses = computed(() => {
// ...
})
defineProps({
value: {
type: String,
required: true
},
focused: {
type: Boolean,
default: false
},
label: String,
icon: String
})
const formattedValue = computed(() => {
// ...
})
const inputClasses = computed(() => {
// ...
})
Single-File Components should always order <script>, <template>, and <style> tags consistently, with <style> last, because at least one of the other two is always necessary.
<style>/* ... */</style> <script>/* ... */</script> <template>...</template>
<!-- ComponentA.vue --> <script>/* ... */</script> <template>...</template> <style>/* ... */</style> <!-- ComponentB.vue --> <template>...</template> <script>/* ... */</script> <style>/* ... */</style>
<!-- ComponentA.vue --> <script>/* ... */</script> <template>...</template> <style>/* ... */</style> <!-- ComponentB.vue --> <script>/* ... */</script> <template>...</template> <style>/* ... */</style>
<!-- ComponentA.vue --> <template>...</template> <script>/* ... */</script> <style>/* ... */</style> <!-- ComponentB.vue --> <template>...</template> <script>/* ... */</script> <style>/* ... */</style>
© 2013–present Yuxi Evan You
Licensed under the MIT License.
https://vuejs.org/style-guide/rules-recommended