Vue實現(xiàn)tab切換的兩種方法示例詳解
Vue常見的實現(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>方法二:屬性綁定+ 動態(tài)組件 component標(biāo)簽
該組件具有一個is屬性,is屬性的值 是 要渲染組件的名字,即為is屬性的值是哪一個組件名,
component 標(biāo)簽就會渲染哪一個組件
缺點:component 可以動態(tài)渲染組件的內(nèi)容,但是每一個切換,都會重新渲染組件內(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>動態(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>
<!--
動態(tài)組件 使用標(biāo)簽 component
該組件具有一個is屬性,is屬性的值 是 要渲染組件的名字,即為is屬性的值是哪一個組件名,
component 標(biāo)簽就會渲染哪一個組件
缺點:component 可以動態(tài)渲染組件的內(nèi)容,但是每一個切換,都會重新渲染組件內(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++">點擊++</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常見的實現(xiàn)tab切換的兩種方法的文章就介紹到這了,更多相關(guān)Vue tab切換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue Cli 環(huán)境刪除與重裝教程 - 版本文檔
這篇文章主要介紹了vue Cli 環(huán)境刪除與重裝教程 - 版本文檔,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
vue-router后臺鑒權(quán)流程實現(xiàn)
本文主要介紹了vue-router后臺鑒權(quán)流程實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08

