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標簽
該組件具有一個is屬性,is屬性的值 是 要渲染組件的名字,即為is屬性的值是哪一個組件名,
component 標簽就會渲染哪一個組件
缺點:component 可以動態(tài)渲染組件的內(nèi)容,但是每一個切換,都會重新渲染組件內(nèi)容,降低渲染效率
使用keep-alive 標簽(組件),可以緩存曾經(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)組件 使用標簽 component 該組件具有一個is屬性,is屬性的值 是 要渲染組件的名字,即為is屬性的值是哪一個組件名, component 標簽就會渲染哪一個組件 缺點:component 可以動態(tài)渲染組件的內(nèi)容,但是每一個切換,都會重新渲染組件內(nèi)容,降低渲染效率 使用keep-alive 標簽(組件),可以緩存曾經(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.js中使用iView日期選擇器并設(shè)置開始時間結(jié)束時間校驗功能
本文通過實例代碼給大家介紹了Vue.js中使用iView日期選擇器并設(shè)置開始時間結(jié)束時間校驗功能,需要的朋友可以參考下2018-08-08關(guān)于vue-router路由的傳參方式params query
這篇文章主要介紹了關(guān)于vue-router路由的傳參方式params query,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10vue深度優(yōu)先遍歷多層數(shù)組對象方式(相當于多棵樹、三級樹)
這篇文章主要介紹了vue深度優(yōu)先遍歷多層數(shù)組對象方式(相當于多棵樹、三級樹),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08使用electron-builder將項目打包成桌面程序的詳細教程
這篇文章主要介紹了使用electron-builder把web端的項目打包生成桌面程序,并可安裝程序,文中通過代碼示例和圖文結(jié)合的方式給大家介紹的非常詳細,具有一定的參考價值,需要的朋友可以參考下2024-08-08