zoukankan      html  css  js  c++  java
  • vue-标签页组件

    content

    <template>
        <div class="tab-content">
            <TabBar v-model="activeKey">
                <TabBody label="标签一" name="1">
                    标签一的内容
                </TabBody>
                <TabBody label="标签二" name="2">
                    标签二的内容
                </TabBody>
                <TabBody label="标签三" name="3">
                    标签三的内容
                </TabBody>
                <TabBody label="标签四" name="4">
                    标签四的内容
                </TabBody>
            </TabBar>
        </div>
    </template>
    
    <script>
    
    import TabBar from '@/components/TabBar';
    import TabBody from '@/components/TabBody';
    
    
    export default {
        name:"tabContent",
        data() {
            return{
                activeKey:'1'
            }
        },
        components:{
            TabBar,
            TabBody
        }
    
    }
    </script>
    
    <style lang="less">
        .tab-header{
            display: flex;
            justify-content: center;
            align-items: center;
            line-height: 30px;
            .tabs-item{
                border-right: 1px solid #eee;
                padding: 0 10px;
                &:last-child{
                    border-right: 0;
                }
            }
        }
        .tab-content{
            padding: 10px;
        }
    </style>

    tabbar

    <template>
        <div class="tab-bar">
            <div class="tab-header">
                <div class="tabs-item" 
                    :class="tabCls(item)"
                    v-for="(item,idx) in navList"
                    :key="idx"
                    @click="handlerChange(idx)"
                >
                {{item.label}}
                </div>
    
            </div>
    
            <div class="tab-body">
                <slot></slot>
            </div>
        </div>
    </template>
    
    <script>
    export default {
        props:{
            value:{
                type:[String,Number]
            }
        },
        data(){
            return{
                currentValue:this.value,
                navList:[]
            }
        },
        methods:{
            tabCls(item){
                return [
                    'tabs-tab',
                    {
                        'tabs-active':item.name == this.currentValue
                    }
                ]
            },
            handlerChange(idx){
                let nav = this.navList[idx];
                let name = nav.name;
                this.currentValue = name;
                this.$emit('input',name)
                this.$emit('on-click',name);
            },
            getTabs(){
                return this.$children.filter((item)=>{
                    return item.$options.name == 'pane'
                })
            },
            updateNav(){
                this.navList = [];
                this.getTabs().forEach((pane,idx)=>{
                    this.navList.push({
                        label:pane.label,
                        name:pane.name || idx
                    })
                    if(!pane.name){
                        pane.nam = idx;
                    }
                    if(idx == 0){
                        if(!this.currentValue){
                            this.currentValue == pane.name || idx;
                        }
                    }
                })
                this.updateStatus();
            },
            updateStatus(){
                let tabs = this.getTabs();
                tabs.forEach(item=>{
                    return item.show = item.name === this.currentValue;
                })
            }
        },
        watch:{
            value(val){
                this.currentValue = val;
            },
            currentValue(){
                this.updateStatus();
            }
        }
    }
    </script>
    
    <style>
    
    </style>

    tab-body

    <template>
        <div class="tab-content" v-show="show">
            <slot></slot>
        </div>
    </template>
    
    <script>
    export default {
        name:'pane',
        props:{
            name:{
                type:String
            },
            label:{
                type:String,
                default:''
            }
        },
        data() {
            return{
                show:true
            }
        },
        methods:{
            updateNav(){
                this.$parent.updateNav();
            }
        },
        mounted(){
            this.updateNav();
        },
        watch:{
            label(){
                this.updateNav();
            }
        }
    }
    </script>
    
    <style>
    
    </style>
  • 相关阅读:
    小公司的程序员,老想跳槽怎么办?
    阿里出品的最新版 Java 开发手册,嵩山版,扫地僧
    程序员的“三十而已”
    应届毕业生,只会抄代码,该怎么办?
    可笑,你竟然不知道 Java 如何生成 UUID
    因为不知道Java的CopyOnWriteArrayList,面试官让我回去等通知
    保姆级教程,如何发现 GitHub 上的优质项目?
    给我半首歌的时间,给你说明白Immutable List
    十分钟色彩科学:LUT 的前世今生
    无源与有源元件的区别
  • 原文地址:https://www.cnblogs.com/huangmin1992/p/10158559.html
Copyright © 2011-2022 走看看