Vue3使用v-if指令進行條件渲染的實例代碼
概述
v-if指令主要用來實現(xiàn)條件渲染,在實際項目中使用得也非常多。
v-if通常會配合v-else-if、v-else指令一起使用,可以達到多個條件執(zhí)行一個,兩個條件執(zhí)行一個,滿足一個條件執(zhí)行等多種場景。
下面,我們分別演示這三種使用場景。
基本用法
我們創(chuàng)建src/components/Demo12.vue,在這個組件中,我們要:
- 場景1:v-if單獨使用,如果count大于0,則顯示“數(shù)字大于0了”。
- 場景2:v-if和v-else配合使用,如果count大于20,則顯示“數(shù)字大于20了”,否則顯示“數(shù)字小于或者等于20”
- 場景3:v-if、v-else-if、v-else配合使用,如果count大于100則顯示“數(shù)字大于100了”,如果count等于100,則顯示“數(shù)字等于100了”,否則顯示“數(shù)字小于100了”
為了便于查看效果,我們還要通過兩個按鈕,一個按鈕控制count的增加,另一個按鈕控制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并進行渲染:
<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>
啟動方式
yarn yarn dev
瀏覽器訪問:http://localhost:5173/
以上就是Vue3使用v-if指令進行條件渲染的實例代碼的詳細內(nèi)容,更多關(guān)于Vue3 v-if條件渲染的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue實現(xiàn)Base64轉(zhuǎn)png、jpg圖片格式
這篇文章主要給大家介紹了關(guān)于Vue實現(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ā)事件的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09vue3接口數(shù)據(jù)賦值對象,渲染報錯問題及解決
這篇文章主要介紹了vue3接口數(shù)據(jù)賦值對象,渲染報錯問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09Vue中watch監(jiān)聽第一次不觸發(fā)、深度監(jiān)聽問題
這篇文章主要介紹了Vue中watch監(jiān)聽第一次不觸發(fā)、深度監(jiān)聽問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10