選單
×
   ❮   
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 透傳屬性 Vue 作用域樣式 Vue 區域性元件 Vue 插槽 Vue v-slot Vue 作用域插槽 Vue 動態元件 Vue Teleport Vue HTTP 請求 Vue 模板引用 Vue 生命週期鉤子 Vue Provide/Inject Vue 路由 Vue 表單輸入 Vue 動畫 Vue v-for 動畫 Vue 構建 Vue 組合式 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 v-else-if 指令


示例

使用 v-else-if 指令在條件為 'true' 時建立 <div> 元素。

<div v-if="word === 'apple'">
  <img src="/img_apple.svg" alt="apple" />
  <p>The value of the 'word' property is 'apple'.</p>
</div>
<div v-else-if="word === 'pizza'">
  <img src="/img_pizza.svg" alt="pizza" />
  <p>The value of the 'word' property is 'pizza'</p>
</div>
執行示例 »

更多示例請參見下方。


定義和用法

v-else-if 指令用於有條件地渲染一個元素。

v-else-if 指令只能在一個帶有 v-if 的元素之後,或者在另一個帶有 v-else-if 的元素之後使用。

當在元素上使用 v-else-if 時,它必須跟一個表示式

  • 如果表示式評估為 'true',則該元素及其所有內容將在 DOM 中建立。
  • 如果表示式評估為 'false',則該元素將被銷燬。

當使用 v-else-if 切換元素時

  • 我們可以使用內建的 <Transition> 元件來為元素進入和離開 DOM 時新增動畫。
  • 生命週期鉤子(如 'mounted' 和 'unmounted')會被觸發。

條件渲染指令

本概述描述了用於條件渲染的不同 Vue 指令是如何協同工作的。

指令 詳情
v-if v-if 可以單獨使用,也可以與 v-else-if 和/或 v-else 結合使用。如果 v-if 中的條件為“true”,則不考慮 v-else-ifv-else
v-else-if 必須在 v-if 或另一個 v-else-if 之後使用。如果 v-else-if 中的條件為“true”,則不考慮後續的 v-else-ifv-else
v-else 如果 if 語句的第一個部分為 false,則會執行此部分。必須放在 if 語句的最後,在 v-ifv-else-if 之後。

更多示例

示例 1

使用 v-else-if 在儲存中只剩下 1、2 或 3 臺打字機時顯示“所剩無幾!”。

<p v-if="typewriterCount>3">
  有貨
</p>

<p v-else-if="typewriterCount>0">
  所剩不多!
</p>

<p v-else>
  缺貨
</p>
自己動手試一試 »

示例 2

使用 v-else-if 在句子包含“burrito”時顯示特定的文字和影像。

<div id="app">
  <div v-if="text.includes('pizza')">
    <p>文字包含“pizza”一詞</p>
    <img src="img_pizza.svg">
  </div>
  <div v-else-if="text.includes('burrito')">
    <p>文字包含“burrito”一詞,但不包含“pizza”</p>
    <img src="img_burrito.svg">
  </div>
  <p v-else>文字中未找到“pizza”或“burrito”一詞</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>
自己動手試一試 »

示例 3

使用 v-else-if 鏈來切換圖片,並使用 <Transition> 元件建立動畫。

App.vue:

<template>
  <h1>mode="out-in"</h1>
  <p>Click the button to get a new image.</p>
  <p>With mode="out-in", the next image is not added until the current image is removed. Another difference from the previous example, is that here we use computed prop instead of a method.</p>
  <button @click="indexNbr++">Next image</button><br>
  <Transition mode="out-in">
    <img src="/img_pizza.svg" v-if="imgActive === 'pizza'">
    <img src="/img_apple.svg" v-else-if="imgActive === 'apple'">
    <img src="/img_cake.svg" v-else-if="imgActive === 'cake'">
    <img src="/img_fish.svg" v-else-if="imgActive === 'fish'">
    <img src="/img_rice.svg" v-else-if="imgActive === 'rice'">
  </Transition>
</template>

<script>
export default {
  data() {
    return {
      imgs: ['pizza', 'apple', 'cake', 'fish', 'rice'],
      indexNbr: 0
    }
  },
  computed: {
    imgActive() {
      if(this.indexNbr >= this.imgs.length) {
        this.indexNbr = 0;
      }
      return this.imgs[this.indexNbr];
    }
  }
}
</script>

<style scoped>
  .v-enter-active {
    animation: swirlAdded 0.7s;
  }
  .v-leave-active {
    animation: swirlAdded 0.7s reverse;
  }
  @keyframes swirlAdded {
    from {
      opacity: 0;
      rotate: 0;
      scale: 0.1;
    }
    to {
      opacity: 1;
      rotate: 360deg;
      scale: 1;
    }
  }
  img {
    width: 100px;
    margin: 20px;
  }
  img:hover {
    cursor: pointer;
  }
</style>
執行示例 »

相關頁面

Vue 教程:Vue v-if 指令

Vue 參考:Vue v-if 指令

Vue 參考:Vue v-else 指令

Vue 教程: Vue 動畫

Vue 教程:Vue 生命週期鉤子


×

聯絡銷售

如果您想將 W3Schools 服務用於教育機構、團隊或企業,請傳送電子郵件給我們
sales@w3schools.com

報告錯誤

如果您想報告錯誤,或想提出建議,請傳送電子郵件給我們
help@w3schools.com

W3Schools 經過最佳化,旨在方便學習和培訓。示例可能經過簡化,以提高閱讀和學習體驗。教程、參考資料和示例會不斷審查,以避免錯誤,但我們無法保證所有內容的完全正確性。使用 W3Schools 即表示您已閱讀並接受我們的使用條款Cookie 和隱私政策

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