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 |
|
| | `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{{item}}
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:
isattribute is used to dynamically specify the currentcomponent should render which child component. Usually combined withelement and dynamic component features, allowing you to switch between different components at runtime based on data.
Example
Toggle Component
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:
isThe 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.
key 属性
-
Type: String or Number
-
Purpose: In Vue.js list rendering, each
v-forelement in the loop needs a uniquekeyattribute.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 onkey.
Example
<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:
keyshould be unique and stable. Stable means thatkeyshould 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.
ref 属性
-
Type: String or Object
-
Purpose:
refis used to register reference information for an element or child component. In real development, you can userefto 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,
refVue provides a corresponding$refsobject, 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.