使用动态组件实现多标签

一、实现效果

在这里插入图片描述

二、相关代码详情

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>动态组件</title>
        <style>
            div {
                width: 250px;
            }
            .tab-button {
                padding: 6px 10px;
                border-top-left-radius: 3px;
                border-top-right-radius: 3px;
                border: solid 1px #ccc;
                cursor: pointer;
                background: #f0f0f0;
                margin-bottom: -1px;
                margin-right: -1px;
            }
            .tab-button:hover {
                background: #e0e0e0;
            }
            /* 当两个类之间无间隙时,只有都包含这两个类时才会显示出相关的效果 */
            .tab-button.active {
                background: #cdcdcd;
            }
            .tab {
                border: solid 1px #ccc;
                padding: 10px;
            }
        </style>
    </head>
    <body>
        <div id="app">
            <button v-for="tab in tabs" :key="tab.title" :class="['tab-button', {active: currentTab === tab.title}]" @click="currentTab = tab.title">{{tab.displayName}}</button>
            <keep-alive>
                <component v-bind:is = "currentTabComponent" class="tab"></component>    
            </keep-alive>
        </div>
        <script src="../../plugings/vue.js"></script>
        <script>
            Vue.component('tab-introduce', {
                data() {
                    return {
                        content: '这是图书介绍'
                    }
                },
                template: '<div><input v-model="content"></div>'
            })
            Vue.component('tab-comment', {
                template: '<div>这是图书评价</div>'
            })
            Vue.component('tab-qa', {
                template: '<div>这是图书问答</div>'
            })
            new Vue({
                el: '#app',
                data: {
                    currentTab: 'introduce',
                    tabs: [
                        {title: 'introduce', displayName: '图书介绍'},
                        {title: 'comment', displayName: '图书评价'},
                        {title: 'qa', displayName: '图书问答'}
                    ]
                },
                computed: {
                    currentTabComponent: function() {
                        return 'tab-' + this.currentTab;
                    }
                }
            })
        </script>
    </body>
</html>

三、关键代码分析

1、使用<keep-alive>元素将动态组件包裹起来,可以在组件进行切换中保持组件的状态,以避免渲染导致的性能问题。
2、组件的动态属性切换可以使用is属性进行切换,is属性的属性值是组件名称。

<keep-alive>
	<component v-bind:is = "currentTabComponent" class="tab"></component>    
</keep-alive>

3、通过计算属性来控制组件的切换,当点击不同的标签时data属性中的currentTab进行改变,此时currentTabComponent的值也会产生相关的改变,即is属性的属性值也会发生相应的改变。

computed: {
    currentTabComponent: function() {
        return 'tab-' + this.currentTab;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值