Vue.js style guide

Linting

We default to eslint-vue-plugin, with the plugin:vue/recommended. Please check this rules for more documentation.

Basic Rules

  1. The service has it's own file
  2. The store has it's own file
  3. Use a function in the bundle file to instantiate the Vue component:

```javascript // bad class { init() { new Component({}) } }

// good document.addEventListener('DOMContentLoaded', () => new Vue({ el: '#element', components: { componentName }, render: createElement => createElement('component-name'), })); ```

  1. Do not use a singleton for the service or the store

```javascript // bad class Store { constructor() { if (!this.prototype.singleton) { // do something } } }

// good class Store { constructor() { // do something } } ```

  1. Use .vue for Vue templates. Do not use %template in HAML.

Naming

  1. Extensions: Use .vue extension for Vue components. Do not use .js as file extension (#34371).
  2. Reference Naming: Use PascalCase for their instances:

```javascript // bad import cardBoard from 'cardBoard.vue'

components: { cardBoard, };

// good import CardBoard from 'cardBoard.vue'

components: { CardBoard, }; ```

  1. Props Naming: Avoid using DOM component prop names.
  2. Props Naming: Use kebab-case instead of camelCase to provide props in templates.

```javascript // bad

// good

// bad

// good ```

Alignment

  1. Follow these alignment styles for the template method:

  2. With more than one attribute, all attributes should be on a new line:

    ```javascript // bad

    // good

    ```

  3. The tag can be inline if there is only one attribute:

    ```javascript // good

    // good

    // bad ```

Quotes

  1. Always use double quotes " inside templates and single quotes ' for all other JS.

``javascript // bad template: `

// good template: <button :class="style">Button</button> ```

Props

  1. Props should be declared as an object

```javascript // bad props: ['foo']

// good props: { foo: { type: String, required: false, default: 'bar' } } ```

  1. Required key should always be provided when declaring a prop

```javascript // bad props: { foo: { type: String, } }

// good props: { foo: { type: String, required: false, default: 'bar' } } ```

  1. Default key should be provided if the prop is not required. Note: There are some scenarios where we need to check for the existence of the property. On those a default key should not be provided.

```javascript // good props: { foo: { type: String, required: false, } }

// good props: { foo: { type: String, required: false, default: 'bar' } }

// good props: { foo: { type: String, required: true } } ```

Data

  1. data method should always be a function

```javascript // bad data: { foo: 'foo' }

// good data() { return { foo: 'foo' }; } ```

Directives

  1. Shorthand @ is preferable over v-on

```javascript // bad

// good ```

  1. Shorthand : is preferable over v-bind

```javascript // bad

// good ```

  1. Shorthand # is preferable over v-slot

```javascript // bad

// good ```

Closing tags

  1. Prefer self closing component tags

```javascript // bad

// good ```

Component usage within templates

  1. Prefer a component's kebab-cased name over other styles when using it in a template

```javascript // bad

// good ```

Ordering

  1. Tag order in .vue file

```

// We don't use scoped styles but there are few instances of this ```

  1. Properties in a Vue Component: Check order of properties in components rule.

:key

When using v-for you need to provide a unique :key attribute for each item.

  1. If the elements of the array being iterated have an unique id it is advised to use it:

```html <div v-for="item in items" :key="item.id"

 <!-- content -->