菜单
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Vue 教程

Vue 首页 Vue Intro Vue Directives Vue v-bind Vue v-if Vue v-show Vue v-for Vue Events Vue v-on Vue Methods Vue Event Modifiers Vue Forms Vue v-model Vue CSS Binding Vue Computed Properties Vue Watchers Vue Templates

Scaling Up

Vue 为什么、如何和设置 Vue 第一个 SFC 页面 Vue 组件 Vue Props Vue v-for 组件 Vue $emit() Vue Fallthrough Attributes Vue Scoped Styling Vue 本地组件 Vue Slots Vue v-slot Vue Scoped Slots Vue 动态组件 Vue Teleport Vue HTTP 请求 Vue 模板引用 Vue 生命周期钩子 Vue Provide/Inject Vue 路由 Vue 表单输入 Vue 动画 Vue v-for 动画 Vue 构建 Vue Composition API

Vue Reference

Vue Built-in Attributes Vue Built-in Components Vue Built-in Elements Vue Component Instance Vue Directives Vue Instance Options Vue Lifecycle Hooks

Vue 示例

Vue Examples Vue Exercises Vue Quiz Vue Server Vue Certificate

Vue <template> 元素


示例

使用内置的 <template> 元素,通过 v-if 指令来切换 HTML 代码块。

<ul>
  <li>Trolltunga</li>
  <template v-if="display">
    <li>Potato Point</li>
    <li>The souks of Marrakech</li>
    <li>Dry Tortugas</li>
    <li>Halong Bay</li>
  </template>
  <li>...</li>
</ul>
运行示例 »

定义和用法

当与 Vue 的 v-ifv-else-ifv-elsev-forv-slot 指令一起使用时,HTML 的 <template> 标签会成为内置的 Vue <template> 元素。

当与 v-ifv-else-ifv-elsev-for 一起使用时,内置的 <template> 元素会渲染一个 HTML 代码块。

当与 v-slot 一起使用时,内置的 <template> 元素会将一个 HTML 代码块定向到一个指定的插槽。请参见下面的示例 1。

内置的 <template> 元素本身不会被渲染为 DOM 元素。

注意: 当使用 SFC(*.vue)文件时,顶层 <template> 元素是结构要求。对于这种顶层的 <template> 元素,不能使用 Vue 指令。


更多示例

示例 1

使用内置的 <template> 元素封装多个元素,并通过 v-slot 指令将它们发送到特定的命名插槽。

<template>
  <h1>App.vue</h1>
  <p>The component has two div tags with one slot in each.</p>
  <slot-comp>
    <template v-slot:bottomSlot>
      <h4>To the bottom slot!</h4>
      <p>This p tag and the h4 tag above are directed to the bottom slot with the v-slot directive used on the template tag.</p>
    </template>
    <p>This goes into the default slot</p>
  </slot-comp>
</template>
运行示例 »

示例 2

使用 <template> 元素,可以使用 v-if 指令渲染多个元素。

<div id="app">
  <template v-if="text.includes('pizza')">
    <p>文本包含“pizza”一词</p>
    <img src="img_pizza.svg">
  </template>
  <p v-else>文本中未找到“pizza”一词</p>
</div>

<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script>
  const app = Vue.createApp({
    data() {
      return {
        text: 'I like taco, pizza, Thai beef salad, pho soup and tagine.'
      }
    }
  })
  app.mount('#app')
</script>
自己动手试一试 »

相关页面

Vue 教程:Vue v-slot

Vue 教程:Vue 模板

Vue 教程:Vue v-if 指令

Vue 参考:Vue v-if 指令

Vue 参考:Vue v-slot 指令

HTML 参考:HTML <template> 标签


×

联系销售

如果您想将 W3Schools 服务用于教育机构、团队或企业,请发送电子邮件给我们
sales@w3schools.com

报告错误

如果您想报告错误,或想提出建议,请发送电子邮件给我们
help@w3schools.com

W3Schools 经过优化,旨在方便学习和培训。示例可能经过简化,以提高阅读和学习体验。教程、参考资料和示例会不断审查,以避免错误,但我们无法保证所有内容的完全正确性。使用 W3Schools 即表示您已阅读并接受我们的使用条款Cookie 和隐私政策

版权所有 1999-2024 Refsnes Data。保留所有权利。W3Schools 由 W3.CSS 提供支持