欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue3使用v-if指令進(jìn)行條件渲染的實(shí)例代碼

 更新時(shí)間:2024年03月26日 11:00:17   作者:Python私教  
條件渲染是根據(jù)條件的真假來(lái)有條件地渲染元素,在Vue.js 3.x中,常見(jiàn)的條件渲染包括使用v-if指令和v-show指令,本文講解使用v-if指令進(jìn)行條件渲染,需要的朋友可以參考下

概述

v-if指令主要用來(lái)實(shí)現(xiàn)條件渲染,在實(shí)際項(xiàng)目中使用得也非常多。

v-if通常會(huì)配合v-else-if、v-else指令一起使用,可以達(dá)到多個(gè)條件執(zhí)行一個(gè),兩個(gè)條件執(zhí)行一個(gè),滿足一個(gè)條件執(zhí)行等多種場(chǎng)景。

下面,我們分別演示這三種使用場(chǎng)景。

基本用法

我們創(chuàng)建src/components/Demo12.vue,在這個(gè)組件中,我們要:

  • 場(chǎng)景1:v-if單獨(dú)使用,如果count大于0,則顯示“數(shù)字大于0了”。
  • 場(chǎng)景2:v-if和v-else配合使用,如果count大于20,則顯示“數(shù)字大于20了”,否則顯示“數(shù)字小于或者等于20”
  • 場(chǎng)景3:v-if、v-else-if、v-else配合使用,如果count大于100則顯示“數(shù)字大于100了”,如果count等于100,則顯示“數(shù)字等于100了”,否則顯示“數(shù)字小于100了”

為了便于查看效果,我們還要通過(guò)兩個(gè)按鈕,一個(gè)按鈕控制count的增加,另一個(gè)按鈕控制count的減少。

代碼如下:

<script setup>
import {ref} from "vue";

const count = ref(33)
</script>
<template>
  <div v-if="count>0">數(shù)字大于0了</div>
  <hr>
  <div v-if="count>20">數(shù)字大于20了</div>
  <div v-else>數(shù)字小于或者等于20</div>
  <hr>
  <div v-if="count>100">數(shù)字大于100了</div>
  <div v-else-if="count===100">數(shù)字等于100了</div>
  <div v-else>數(shù)字小于100了</div>
  <hr>
  <div>
    <h3>{{ count }}</h3>
    <button @click="count+=10">增加</button>
    <button @click="count-=10">減少</button>
  </div>
</template>

接著,我們修改src/App.vue,引入Demo12.vue并進(jìn)行渲染:

<script setup>
import Demo from "./components/Demo12.vue"
</script>
<template>
  <h1>歡迎跟著Python私教一起學(xué)習(xí)Vue3入門(mén)課程</h1>
  <hr>
  <Demo/>
</template>

然后,我們?yōu)g覽器訪問(wèn):http://localhost:5173/

在這里插入圖片描述

完整代碼

package.json

{
  "name": "hello",
  "private": true,
  "version": "0.1.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build"
  },
  "dependencies": {
    "vue": "^3.3.8"
  },
  "devDependencies": {
    "@vitejs/plugin-vue": "^4.5.0",
    "vite": "^5.0.0"
  }
}

vite.config.js

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
  plugins: [vue()],
})

index.html

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="/vite.svg" rel="external nofollow"  />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vite + Vue</title>
  </head>
  <body>
    <div id="app"></div>
    <script type="module" src="/src/main.js"></script>
  </body>
</html>

src/main.js

import { createApp } from 'vue'
import App from './App.vue'

createApp(App).mount('#app')

src/App.vue

<script setup>
import Demo from "./components/Demo12.vue"
</script>
<template>
  <h1>歡迎跟著Python私教一起學(xué)習(xí)Vue3入門(mén)課程</h1>
  <hr>
  <Demo/>
</template>

src/components/Demo12.vue

<script setup>
import {ref} from "vue";

const count = ref(33)
</script>
<template>
  <div v-if="count>0">數(shù)字大于0了</div>
  <hr>
  <div v-if="count>20">數(shù)字大于20了</div>
  <div v-else>數(shù)字小于或者等于20</div>
  <hr>
  <div v-if="count>100">數(shù)字大于100了</div>
  <div v-else-if="count===100">數(shù)字等于100了</div>
  <div v-else>數(shù)字小于100了</div>
  <hr>
  <div>
    <h3>{{ count }}</h3>
    <button @click="count+=10">增加</button>
    <button @click="count-=10">減少</button>
  </div>
</template>

啟動(dòng)方式

yarn
yarn dev

瀏覽器訪問(wèn):http://localhost:5173/

以上就是Vue3使用v-if指令進(jìn)行條件渲染的實(shí)例代碼的詳細(xì)內(nèi)容,更多關(guān)于Vue3 v-if條件渲染的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論