欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue實現(xiàn)tab切換的兩種方法示例詳解

 更新時間:2023年11月28日 09:25:56   作者:小姚學前端  
這篇文章主要介紹了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)文章

最新評論