Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue 3 Built-in Attributes

Vue 3 Built-in Attributes

In Vue.js, is, key, and ref are three commonly used built-in attributes, used as follows.

property Core Role Usage/Example
is Solves HTML tag syntax limitations; dynamically specifies the rendered component/native tag


| | `key` | Provides a unique identity for the Vue virtual DOM, improving list rendering efficiency and ensuring correct component state |
{{item}}
| | `ref` | Obtains a DOM element or component instance, for directly manipulating the native DOM / calling child component methods | Template: ref="name" Script: this.$refs.name


this.$refs.inputRef.focus()
|

is 属性

In Vue.js, the is attribute is typically used for implementing dynamic components, and is especially useful when you want to dynamically switch between different components at runtime.

  • Type: String or Object
  • Purpose: is attribute is used to dynamically specify the current component should render which child component. Usually combined with element and dynamic component features, allowing you to switch between different components at runtime based on data.

Example

In the code above,currentComponent is a data property that determines element's specific component to render. When the button is clicked, calling the toggleComponent method switches the currentComponent value, thereby dynamically changing the rendered component.

Notes:

  • Notes:
    • is The value of the attribute can be a component name string or a component object.
    • When using a string, the component name should match the registered component name.
    • If using an object, you can directly reference the component object or use a dynamic computed property to return the component object.
    - The value of the `is` attribute can be a component name string or a component object. - When using a string, the component name should match the registered component name. - If using an object, you can directly reference the component object or use a dynamic computed property to return the component object.

    key 属性

    • Type: String or Number

    • Purpose: In Vue.js list rendering, each v-for element in the loop needs a unique key attribute.key 's role is to help Vue identify the uniqueness of each node, thereby efficiently updating the virtual DOM. When Vue re-renders the list, it decides whether to reuse existing DOM elements or recreate them based on key .

    Example

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

    In the code above,item.id as key attribute ensures each list item can be correctly identified and handled by Vue when data updates.

    -

    Notes:

    • key should be unique and stable. Stable means thatkey should not change with the data; otherwise it affects Vue's performance and the virtual DOM reuse strategy.
    • Avoid using the index as keyunless you are certain the list item order will not change.
    - `key` should be unique and stable. Stable means `key` should not change with the data; otherwise it affects Vue's performance and the virtual DOM reuse strategy. - Avoid using the index as `key` unless you are certain the list item order will not change.

    ref 属性

    • Type: String or Object

    • Purpose: ref is used to register reference information for an element or child component. In real development, you can use ref to access the specific DOM element or child component instance, enabling operations such as calling methods and accessing properties.

    Add a ref to a DOM Element

    Example

    Add a ref to a Child Component

    Example

    Notes:

    • When used in a template, ref Vue provides a corresponding $refs object, through which you can access the registered DOM element or child component instance.
    • Avoid Overuse ref, operate on the DOM and child components as much as possible through data-driven approaches and event handling, keeping the code maintainable and clear.