YouTip LogoYouTip

Vue3 Api Template

Vue3 Template Tag <template> | Rookie Tutorial

Rookie Tutorial --

Vue3 Tutorial

Reference Manual

Practical Case 1

Practical Case 2

Vue3 Global API

Vue3 Quiz

Deep Exploration

Web Design and Development

Programming Languages

Web Services

Development Tools

Programming

Scripting Languages

Scripts

Computer Science

Web Service

Software

Vue3 Template Tag <template>


Image 3: Vue3 Options API Vue3 Options API

In Vue3, the <template> tag is a very core concept.

<template> is used to define the template structure of a component and is the foundation for Vue to render pages.


Basic Concepts of <template> Tag

The <template> tag is used in Vue3 to define component templates. It itself will not be rendered to the final DOM, but serves as a container for wrapping Vue's template syntax (such as interpolation, directives, etc.). Vue will generate actual HTML structure based on the content in <template>.

Features

  1. Non-rendering tag: The <template> tag itself will not appear in the final DOM structure.
  2. Template container: It is used to wrap Vue's template syntax, such as interpolation, conditional rendering, list rendering, etc.
  3. Supports multiple root nodes: In Vue3, the <template> tag can contain multiple root nodes, while in Vue2 only one was allowed.

Usage Scenarios of <template> Tag

1. Defining Component Templates

In Single File Components (.vue files), the <template> tag is used to define the template part of the component. For example:

Example

<template>
  <div>
    <h1>{{ title }}</h1>
    <p>{{ description }}</p>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        title: "Hello Vue3",
        description: "This is a Vue3 template example."
      };
    }
  };
</script>

2. Conditional Rendering

The <template> tag can be used with v-if or v-else directives to achieve conditional rendering. For example:

Example

<template>
  <div>
    <template v-if="isVisible">
      <p>This content is visible.</p>
    </template>
  </div>
</template>
← Vue3 Api CompileroptionsVue3 Api Emits β†’