使用vue實現tab操作


在使用jQuery類庫實現tab功能時,是獲取鼠標在mousenter或click時的index值,然后切換到當前的標題和內容,把其他的標題和內容的狀態去掉:

$('.tab .title').find('.item')
    .removeClass('current').eq(index).addClass('current'); // 為index位置的title添加current
$('.tab .content').find('.item')
    .hide().eq(index).show(); // 顯示index位置的內容

那么在使用vue實現tab功能時,就不是像jQuery這種直接操作DOM了。我這里總結了下實現tab功能的3個思路,僅供參考。

1. 切換content或者直接切換內容

這種思路下,我們首先把結構搭建起來,然后用一個變量selected表示tab當前展示的位置,給li標簽添加mouseenter或click事件,將當前的index傳遞進去:
html代碼:

<div class="hd">
    <ul class="clearfix">
        <li v-for="(item, index) of list" :class="{active:selected==index}" @mouseenter="change(index)">{{item.title}}</li>
    </ul>
</div>
<div v-for="(item, index) of list" :class="{active:selected==index, item:true}" v-html="item.content"></div>

js代碼:

var app = new Vue({
    el: '#app',
    data: {
        selected: 0, //當前位置
        list: [
            {
                title: '11111',
                content: '11111content'
            },
            {
                title: '22222',
                content: '222222content'
            },
            {
                title: '33333',
                content: `<div>
                                <span style="color:#f00">hello world</span>
                                <p><input type="text" v-model="message"></p>
                                <p>{{message}}</p>
                            </div>`
            }
        ]
    },
    methods: {
        change(index) {
            this.selected = index;
        }
    }
})

綁定的change(index)事件,每次都將index給了selected,然后tab就會切換到對應的標簽。【查看實例1

上面的代碼里,我們是通過切換div的顯示與隱藏來進行執行的。tab中的content里如果只有純html內容,我們可以直接把list[selected].content展示到.bd中:

<div class='bd' v-html="list[selected].content"></div>

每次selected變換時,bd的內容都會發生變化。

2. 使用currentView

在上面的實現方式中,第3個tab里有個輸入框與p標簽雙向綁定,但是沒有效果,因為vue是把list中的內容作為html元素填充到頁面中的,message並沒有作為vue的屬性綁定給input。那么使用組建和currentView就能彌補這個缺陷。

無論使用全局注冊還是局部注冊的組件,思路都是一樣的,我們暫時使用全局注冊的組件來實現。

每個組件里展示的是一個tab里的內容,先注冊3個組件:

// tab0
Vue.component('item0',{
    template : '<div>1111111content</div>'
});
// tab1
Vue.component('item1',{
    template : '<div>222222content</div>'
})
// tab2
Vue.component('item2',{
    data(){
        return{
            message : ''
        }
    },
    template : `<div>
                    <span style="color:#f00">hello world</span>
                    <p><input type="text" v-model="message"></p>
                    <p>{{message}}</p>
                </div>`
})

然后在html中使用component來展示對應組件的內容,title的展示方式不變:

<div class="hd">
    <ul class="clearfix">
        <li v-for="(item, index) of list" :class="{active:selected==index}" @mouseenter="change(index)">{{item.title}}</li>
    </ul>
</div>
<component :is="currentView"></component>

currentView屬性可以讓多個組件可以使用同一個掛載點,並動態切換:

var app = new Vue({
    el: '#app',
    data: {
        selected: 0,
        currentView : 'item0',
        list: [
            {
                title: '11111'
            },
            {
                title: '22222'
            },
            {
                title: '33333'
            }
        ]
    },
    methods: {
        change(index) {
            this.selected = index;
            this.currentView = 'item'+index; // 切換currentView
        }
    }
})

這樣 message 在組件里就是一個獨立的data屬性,能在tab里也使用vue綁定事件了【查看實例2】。

原文地址: https://www.xiabingbao.com/vue/2017/07/02/vue-tab.html

3. 使用slot方式等

使用slot方式進行內容分發或者一個獨立的組件,可以讓我們把代碼整合到一塊,對外提供一個數據接口,只要按照既定的格式填寫數據即可。

3.1 slot

用slot方式寫一個子組件:

Vue.component('my-slot-tab', {
    props : ['list', 'selected'],
    template : `<div class="tab">
                    <div class="hd">
                        <ul class="clearfix">
                            <slot name="title" v-for="(item, index) in list" :index="index" :text="item.title"> </slot>
                        </ul>
                    </div>
                    <div class="bd">
                        <slot name="content" :content="list[selected].content"></slot>
                    </div>
                </div>`
});

父組件模板:

<my-slot-tab :list="list" :selected="selected">
    <template slot="title" scope="props">
        <li :class="{active:selected==props.index, item:true}" @mouseenter="change(props.index)">{{ props.text }}</li>
    </template>
    <template slot="content" scope="props">
        <div v-html="props.content"></div>
    </template>
</my-slot-tab>

父組件中slot="title"會替換子組件中name="title"的slot,父組件中slot="content"會替換子組件中name="content"的slot.最終渲染出來的tab結構與上面之前的代碼一樣。【查看實例3-1

3.2 其他組件方式

還有一種方式就是把所有的模板都寫到組件中。
子組件:

Vue.component('my-tab', {
    props : ['list'],
    template : `<div class="tab">
                    <div class="hd">
                        <ul class="clearfix">
                            <li v-for="(item, index) in list" :class="{active:selected==index, item:true}" @mouseenter="change(index)">{{item.title}}</li>
                        </ul>
                    </div>
                    <div class="bd">
                        <div v-for="(item, index) of list" :class="{active:selected==index, item:true}" v-html="item.content"></div>
                    </div>
                </div>`,
    data(){
        return{
            selected:0
        }
    },
    methods : {
        change(index){
            this.selected = index;
        }
    }
});

父組件:

<my-tab :list="list"></my-tab>  

這種只需要傳遞一個list即可。【查看實例3-2

對比這兩種方法,slot中可以自定義更多的內容,而下面的方法使用起來更加簡單,只是自定義的東西比較少。

4. 總結

上面講解了幾種實現tab功能的方式,沒有說哪種方式最好,選擇最適合自己項目需求的方式就是最好的。文中有哪有錯誤或不足,歡迎批評指正。

原文地址: https://www.xiabingbao.com/vue/2017/07/02/vue-tab.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM