Vue 3 is Attribute
is The is attribute is the core attribute for implementing dynamic components in Vue, used to dynamically specify which component the current element should render as.
Basic Explanation
is The is attribute is mainly used in dynamic component scenarios, allowing you to dynamically switch between different components for rendering at runtime based on data. This is the primary way to implement dynamic component functionality in Vue.
- Type: String | Component
- Purpose: Dynamically switch components, achieving component reuse and dynamic rendering.
Basic Usage
1. Dynamic Component Switching
Use The element works with is attribute to implement dynamic components:
Example
Switch component
2. Using a Component Object
The value of is can also be a component object directly:
Example
Show A
Show B
Use Cases
1. Tab Switching
Achieve an effect similar to tab switching:
Example
2. Conditionally Rendering Different Components
Dynamically select the component to render based on conditions:
Example
Using is on native elements
In Vue 3.1+, when is the attribute is used on a native HTML element, it is interpreted as the native Web platform's custom element feature.
But sometimes you need to replace a native element with a Vue component in a DOM template. For example, in Use 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. Using computed properties: can return different components based on complex conditional logic, enabling more flexible dynamic component switching. Use Home Profile , using a custom component as a row:
Example
vue: prefix lets Vue render the element as a Vue component rather than a native HTML element.
Notes
Caching dynamic components with keep-alive
Wrapping dynamic components can cache component instances, avoiding re-creation on each switch:Example
will cache the switched component instances; when the component is displayed again, it is restored directly from the cache rather than re-created.
Vue 3 Built-in Attributes