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í)行等多種場景。
下面,我們分別演示這三種使用場景。
基本用法
我們創(chuàng)建src/components/Demo12.vue,在這個(gè)組件中,我們要:
- 場景1:v-if單獨(dú)使用,如果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了”
為了便于查看效果,我們還要通過兩個(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條件渲染的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue.js 利用v-for中的index值實(shí)現(xiàn)隔行變色
這篇文章主要介紹了Vue.js 利用v-for中的index值實(shí)現(xiàn)隔行變色效果,首先定義好樣式,利用v-for中的index值,然后綁定樣式來實(shí)現(xiàn)隔行變色,需要的朋友可以參考下2018-08-08
vue結(jié)合g6實(shí)現(xiàn)樹級結(jié)構(gòu)(compactBox?緊湊樹)
本文主要介紹了vue結(jié)合g6實(shí)現(xiàn)樹級結(jié)構(gòu),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
keep-Alive搭配vue-router實(shí)現(xiàn)緩存頁面效果的示例代碼
這篇文章主要介紹了keep-Alive搭配vue-router實(shí)現(xiàn)緩存頁面效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
解決vue 給window添加和移除resize事件遇到的坑
這篇文章主要介紹了解決vue 給window添加和移除resize事件遇到的坑,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
vite+vue3+pinia實(shí)現(xiàn)動(dòng)態(tài)注冊懶加載路由教程
這篇文章主要介紹了vite+vue3+pinia實(shí)現(xiàn)動(dòng)態(tài)注冊懶加載路由教程,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04

