Vue tab组件

今天跟大家分享一个tab切换的组件,功能相对完善。
话不多说,直接上代码:

<tab-content :list=elList :fnoptions=optionsFn>
              <div class= slot=0>
                     <the-all-game :list=gameAllList></the-all-game>
              </div>
              <div class= slot=1>
                     <the-collect :list=gameCollectList></the-collect>
             </div></tab-content>

slot里面是tab的内容。

script代码:

data () {      elList: [
                {                    id: 0,                    name: '全部游戏'
                },
                {                    id: 1,                    name: '我的最爱'
                }
            ],       optionsFn: [
                {runFn:() => console.log('click tab1')},
                {runFn:() => console.log('click tab2')}
            ],
}

以上就是tab头部要显示的内容,及点击tab后触发的回调函数。

接下来看看组件里面如何写的:

<template>
    <div class=container>
        <div class=header>
            <span class=title :class=index === active? 'active' : '' v-for=(item, index) in tabList :key=index @click=open(item.id)>{{item.name}}</span>
        </div>
        <div class=content>
                <div class=wrap>
                    <slot :name=active></slot>
                </div>
        </div>
    </div></template><script>export default {    name: 'tab-content',
    data () {        return {            active: 0,            tabList: this.list,            runfn: this.fnoptions
        };
    },    components: {        // ScrollComponent
    },    props: {        list: {            type: Array,            default () {                return [
                    {                        id: 0,                        name: '页面一'
                    },
                    {                        id: 1,                        name: '页面二'
                    }
                ];
            }
        },        fnoptions: {            type: Array,            default () {                return 【】;
            }
       }     
    },    methods: {
        open (id) {            this.active = id;            if (this.runfn[id]) {                this.runfn[id].runFn();
            }
        }
    }
};</script>

想必看到这里大家也该明白了,就是通过组件的slot控制显影。

作者:我是上帝可爱多

相关文章

https://segmentfault.com/a/1190000022018995 https://www....
ES6 (ECMAScript 6)中的模块是一个包含 JavaScript 代码的...
from https://mp.weixin.qq.com/s/-rc1lYYlsfx-wR4mQmIIQQ V...
D:\Temp&gt;npm init vite@latest vue3study --temp...
文章浏览阅读1.2k次。最近自己从零撸起的甘特图组件需要子组...
文章浏览阅读3.3k次,点赞3次,收藏16次。静默打印是什么?简...