Vue3 Template Tag
In Vue 3, The tag is a very core concept.
is used to define the component's template structure and is the foundation of Vue's page rendering.
Basic Concept of the Tag
The tag is used in Vue 3 to define the component template. It is not rendered into the final DOM itself; instead, it serves as a container that wraps Vue's template syntax (such as interpolation, directives, etc.). Vue generates the actual HTML structure based on the content of .
Feature
- Non-Rendering Tag:
The tag itself does not appear in the final DOM structure. - Template Container: It is used to wrap Vue's template syntax, such as interpolation, conditional rendering, list rendering, and so on.
- Supports Multiple Root Nodes: In Vue 3,
tags can contain multiple root nodes, whereas in Vue 2 only one was allowed.
Use Cases for the Tag
1. Define the Component Template
In a Single File Component (.vue file), the tag is used to define the component's template section. For example:
Example
{{ title }}
{{ description }}
2. Conditional Rendering
The tag can be used together with the v-if or v-else directive to implement conditional rendering. For example:
Example
This content is visible.
This content is hidden.
3. List Rendering
The tag can be used together with the v-for directive to implement list rendering. For example:
Example
-
{{ item.name }}
Notes on the Tag
- Does Not Support Dynamic Attributes:
The tag itself does not support dynamic attributes (such as:classor:style, etc.), because it is ignored during rendering. - Avoid Nested Usage: Although
The tag can be nested, but excessive nesting may reduce code readability. - The difference between
v-slotCooperating with: In Vue 3,The tag is commonly used to define slot content, for example:
Example
Header Content
Default Content
Vue3 Options API