Vue實(shí)現(xiàn)tab切換的兩種方法示例詳解
Vue常見的實(shí)現(xiàn)tab切換的兩種方法
方法一:事件綁定+屬性綁定 效果圖

完整代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>tab</title>
<style>
*{
margin: 0;
padding: 0;
}
h2{
width: 500px;
height: 400px;
background: #9fe4d9;
text-align: center;
line-height: 400px;
display: none;
}
#app .kk{
width: 500px;
height: 50px;
display: flex;
justify-content: space-between;
}
#app{
width: 500px;
margin: 50px auto;
}
span{
flex: 1;
text-align: center;
line-height: 50px;
background: #ccc;
}
.on{
background: pink;
}
#app .onn{
display: block;
}
</style>
</head>
<body>
<div id="app">
<div class="kk">
<span :class =" n==1 && 'on'" @click.self="n=1">1</span>
<span :class =" n==2 && 'on'" @click.self="n=2">2</span>
<span :class =" n==3 && 'on'" @click.self="n=3">3</span>
<span :class =" n==4 && 'on'" @click.self="n=4">4</span>
<span :class =" n==5 && 'on'" @click.self="n=5">5</span>
</div>
<h2 :class =" n==1 && 'onn'">1</h2>
<h2 :class =" n==2 && 'onn'">2</h2>
<h2 :class =" n==3 && 'onn'">3</h2>
<h2 :class =" n==4 && 'onn'">4</h2>
<h2 :class =" n==5 && 'onn'">5</h2>
</div>
</body>
</html>
<script type="module">
import {createApp} from './js/vue.esm-browser.js';
createApp({
data() {
return {
n:1
}
},
methods:{
},
}).mount('#app')
</script>方法二:屬性綁定+ 動(dòng)態(tài)組件 component標(biāo)簽
該組件具有一個(gè)is屬性,is屬性的值 是 要渲染組件的名字,即為is屬性的值是哪一個(gè)組件名,
component 標(biāo)簽就會(huì)渲染哪一個(gè)組件
缺點(diǎn):component 可以動(dòng)態(tài)渲染組件的內(nèi)容,但是每一個(gè)切換,都會(huì)重新渲染組件內(nèi)容,降低渲染效率
使用keep-alive 標(biāo)簽(組件),可以緩存曾經(jīng)渲染過的組件,從而提高渲染效率
效果圖

完整代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>動(dòng)態(tài)組件</title>
<style>
*{
margin: 0;
padding: 0;
}
.wp{
width: 440px;
height: 30px;
margin: 20px auto;
display: flex;
background: #f0f0f0;
}
.wp span{
flex: 1;
height: 30px;
text-align: center;
line-height: 30px;
cursor: pointer;
}
.wp span.on{
background: pink;
color: #fff;
}
h1{
width: 440px;
margin: 0 auto;
}
</style>
</head>
<body>
<div id="app">
<div class="wp">
<span :class="num==1&&'on'" @click="num=1">水滸傳</span>
<span :class="num==2&&'on'" @click="num=2">紅樓夢</span>
<span :class="num==3&&'on'" @click="num=3">西游記</span>
<span :class="num==4&&'on'" @click="num=4">三國演義</span>
</div>
<!--
動(dòng)態(tài)組件 使用標(biāo)簽 component
該組件具有一個(gè)is屬性,is屬性的值 是 要渲染組件的名字,即為is屬性的值是哪一個(gè)組件名,
component 標(biāo)簽就會(huì)渲染哪一個(gè)組件
缺點(diǎn):component 可以動(dòng)態(tài)渲染組件的內(nèi)容,但是每一個(gè)切換,都會(huì)重新渲染組件內(nèi)容,降低渲染效率
使用keep-alive 標(biāo)簽(組件),可以緩存曾經(jīng)渲染過的組件,從而提高渲染效率
-->
<keep-alive>
<component :is="'comp'+num"></component>
</keep-alive>
</div>
</body>
</html>
<script type="module">
import { createApp } from './js/vue.esm-browser.js';
let comp1={
template:'<h1>水滸傳</h1>'
}
let comp2={
template:'<h1>紅樓夢</h1>'
}
let comp3={
template:`
<h1>西游記</h1>
<p>{{n}}</p>
<button @click = "n++">點(diǎn)擊++</button>
`,
data() {
return {
n:100,
}
},
}
let comp4={
template:'<h1>三國演義</h1>'
}
let aa = {
template:'<h1>金瓶梅</h1>'
}
createApp({
data() {
return {
num:1,
}
},
components:{
comp1,comp2,comp3,comp4,aa
}
}).mount('#app')
</script>到此這篇關(guān)于Vue常見的實(shí)現(xiàn)tab切換的兩種方法的文章就介紹到這了,更多相關(guān)Vue tab切換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue使用動(dòng)態(tài)組件實(shí)現(xiàn)TAB切換效果完整實(shí)例
- vue實(shí)現(xiàn)tab欄切換效果
- Vue中的table表單切換實(shí)現(xiàn)效果
- vue+iview?Table表格多選切換分頁保持勾選狀態(tài)
- vue?router如何實(shí)現(xiàn)tab切換
- vue實(shí)現(xiàn)鼠標(biāo)滑動(dòng)展示tab欄切換
- Vue實(shí)現(xiàn)選項(xiàng)卡tab切換制作
- vue實(shí)現(xiàn)tab切換的3種方式及切換保持?jǐn)?shù)據(jù)狀態(tài)
- vue實(shí)現(xiàn)tab路由切換組件的方法實(shí)例
相關(guān)文章
Vue.js中使用iView日期選擇器并設(shè)置開始時(shí)間結(jié)束時(shí)間校驗(yàn)功能
本文通過實(shí)例代碼給大家介紹了Vue.js中使用iView日期選擇器并設(shè)置開始時(shí)間結(jié)束時(shí)間校驗(yàn)功能,需要的朋友可以參考下2018-08-08
關(guān)于vue-router路由的傳參方式params query
這篇文章主要介紹了關(guān)于vue-router路由的傳參方式params query,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
vue深度優(yōu)先遍歷多層數(shù)組對(duì)象方式(相當(dāng)于多棵樹、三級(jí)樹)
這篇文章主要介紹了vue深度優(yōu)先遍歷多層數(shù)組對(duì)象方式(相當(dāng)于多棵樹、三級(jí)樹),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
使用electron-builder將項(xiàng)目打包成桌面程序的詳細(xì)教程
這篇文章主要介紹了使用electron-builder把web端的項(xiàng)目打包生成桌面程序,并可安裝程序,文中通過代碼示例和圖文結(jié)合的方式給大家介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-08-08

