Priority C Rules: Recommended

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:

  1. Train your brain to more easily parse most of the community code you encounter
  2. Be able to copy and paste most community code examples without modification
  3. Often find new hires are already accustomed to your preferred coding style, at least in regards to Vue

Component/instance options order

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.

  1. Global Awareness (requires knowledge beyond the component)

  2. name

  3. Template Compiler Options (changes the way templates are compiled)

  4. compilerOptions

  5. Template Dependencies (assets used in the template)

  6. components

  7. directives

  8. Composition (merges properties into the options)

  9. extends

  10. mixins
  11. provide/inject

  12. Interface (the interface to the component)

  13. inheritAttrs

  14. props
  15. emits
  16. expose

  17. Composition API (the entry point for using the Composition API)

  18. setup

  19. Local State (local reactive properties)

  20. data

  21. computed

  22. Events (callbacks triggered by reactive events)

  23. watch

  24. Lifecycle Events (in the order they are called)

    • beforeCreate
    • created
    • beforeMount
    • mounted
    • beforeUpdate
    • updated
    • activated
    • deactivated
    • beforeUnmount
    • unmounted
    • errorCaptured
    • renderTracked
    • renderTriggered
    • serverPrefetch (SSR only)
  25. Non-Reactive Properties (instance properties independent of the reactivity system)

  26. methods

  27. Rendering (the declarative description of the component output)

    • template/render

Element attribute order

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.

  1. Definition (provides the component options)

  2. is

  3. List Rendering (creates multiple variations of the same element)

  4. v-for

  5. Conditionals (whether the element is rendered/shown)

  6. v-if

  7. v-else-if
  8. v-else
  9. v-show
  10. v-cloak

  11. Render Modifiers (changes the way the element renders)

  12. v-pre

  13. v-once

  14. Global Awareness (requires knowledge beyond the component)

  15. id

  16. Unique Attributes (attributes that require unique values)

  17. ref

  18. key

  19. Two-Way Binding (combining binding and events)

  20. v-model

  21. Other Attributes (all unspecified bound & unbound attributes)

  22. Events (component event listeners)

  23. v-on

  24. Content (overrides the content of the element)

    • v-html
    • v-text

Empty lines in component/instance options

You 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.

props: {
  value: {
    type: String,
    required: true
  },

  focused: {
    type: Boolean,
    default: false
  },

  label: String,
  icon: String
},

computed: {
  formattedValue() {
    // ...
  },

  inputClasses() {
    // ...
  }
}
// No spaces are also fine, as long as the component
// is still easy to read and navigate.
props: {
  value: {
    type: String,
    required: true
  },
  focused: {
    type: Boolean,
    default: false
  },
  label: String,
  icon: String
},
computed: {
  formattedValue() {
    // ...
  },
  inputClasses() {
    // ...
  }
}
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 component top-level element order

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.

```vue-html [ComponentX.vue]


```vue-html [ComponentA.vue]
<script>/* ... */</script>
<template>...</template>
<style>/* ... */</style>

```vue-html [ComponentB.vue]


```vue-html [ComponentA.vue]
<script>/* ... */</script>
<template>...</template>
<style>/* ... */</style>

```vue-html [ComponentB.vue]


or

```vue-html [ComponentA.vue]
<template>...</template>
<script>/* ... */</script>
<style>/* ... */</style>

```vue-html [ComponentB.vue]

```



url: /style-guide/rules-use-with-caution.md