# Special Attributes

# key

  • Expects: number | string

    The key special attribute is primarily used as a hint for Vue's virtual DOM algorithm to identify VNodes when diffing the new list of nodes against the old list. Without keys, Vue uses an algorithm that minimizes element movement and tries to patch/reuse elements of the same type in-place as much as possible. With keys, it will reorder elements based on the order change of keys, and elements with keys that are no longer present will always be removed/destroyed.

    Children of the same common parent must have unique keys. Duplicate keys will cause render errors.

    The most common use case is combined with v-for:

      <li v-for="item in items" :key="item.id">...</li>

    It can also be used to force replacement of an element/component instead of reusing it. This can be useful when you want to:

    • Properly trigger lifecycle hooks of a component
    • Trigger transitions

    For example:

      <span :key="text">{{ text }}</span>

    When text changes, the <span> will always be replaced instead of patched, so a transition will be triggered.

# ref

  • Expects: string | Function

    ref is used to register a reference to an element or a child component. The reference will be registered under the parent component's $refs object. If used on a plain DOM element, the reference will be that element; if used on a child component, the reference will be component instance:

    <!-- vm.$refs.p will be the DOM node -->
    <p ref="p">hello</p>
    <!-- vm.$refs.child will be the child component instance -->
    <child-component ref="child"></child-component>
    <!-- When bound dynamically, we can define ref as a callback function, passing the element or component instance explicitly -->
    <child-component :ref="(el) => child = el"></child-component>

    An important note about the ref registration timing: because the refs themselves are created as a result of the render function, you cannot access them on the initial render - they don't exist yet! $refs is also non-reactive, therefore you should not attempt to use it in templates for data-binding.

  • See also: Child Component Refs

# is

  • Expects: string | Object (component’s options object)

Used for dynamic components.

For example:

<!-- component changes when currentView changes -->
<component :is="currentView"></component>

Deployed on Netlify.
Last updated: 2020-09-19, 12:45:02 UTC