選單
×
   ❮   
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 提供支援