Vue3使用v-if指令進(jìn)行條件渲染的實(shí)例代碼
概述
v-if指令主要用來實(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了”
為了便于查看效果,我們還要通過兩個(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入門課程</h1> <hr> <Demo/> </template>
然后,我們?yōu)g覽器訪問: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入門課程</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
瀏覽器訪問: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)文章
Vue首屏性能優(yōu)化組件知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理了一篇關(guān)于Vue首屏性能優(yōu)化組件知識(shí)點(diǎn)總結(jié),有需要的朋友們可以跟著學(xué)習(xí)下。2021-11-11vue實(shí)現(xiàn)消息無縫滾動(dòng)效果
這篇文章介紹了vue實(shí)現(xiàn)消息無縫滾動(dòng)效果的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06Vue實(shí)現(xiàn)Base64轉(zhuǎn)png、jpg圖片格式
這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)Base64轉(zhuǎn)png、jpg圖片格式的相關(guān)資料,前段獲取生成的是base64圖片,需要轉(zhuǎn)化為jpg,png,需要的朋友可以參考下2023-09-09解決Vue.js父組件$on無法監(jiān)聽子組件$emit觸發(fā)事件的問題
今天小編就為大家分享一篇解決Vue.js父組件$on無法監(jiān)聽子組件$emit觸發(fā)事件的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09vue3接口數(shù)據(jù)賦值對(duì)象,渲染報(bào)錯(cuò)問題及解決
這篇文章主要介紹了vue3接口數(shù)據(jù)賦值對(duì)象,渲染報(bào)錯(cuò)問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09Vue中watch監(jiān)聽第一次不觸發(fā)、深度監(jiān)聽問題
這篇文章主要介紹了Vue中watch監(jiān)聽第一次不觸發(fā)、深度監(jiān)聽問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10