首页 > 其他分享 >Vue--Tab栏切换(父子组件间传值实现)

Vue--Tab栏切换(父子组件间传值实现)

时间:2022-10-28 19:57:51浏览次数:62  
标签:index Vue background -- height color Tab tempIndex 组件

一、实现原理:

子组件配置props属性接受父组件传来的index值,top子组件采用this.$emit方法传index值给父组件

二、HTML代码:
<div class="box">
    <my-top @xxx="fnChange"></my-top>
    <my-body :curindex="curindex"></my-body>
</div>
三、CSS代码:
* {
    margin: 0;
    padding: 0;
}

ul {
    list-style-type: none;
}

.box {
    width: 400px;
    height: 300px;
    border: 1px solid #ccc;
    margin: 100px auto;
    overflow: hidden;
}

.hd {
    height: 45px;
}

.hd span {
    display: inline-block;
    width: 90px;
    background-color: pink;
    line-height: 45px;
    text-align: center;
}

.hd span.current {
    background-color: purple;
    color: white;
}

.bd li {
    height: 255px;
    background-color: purple;
    display: none;
    color: white;
}

.bd li.current {
    display: block;
}
四、JS代码:
let vm = new Vue({
    el: ".box",
    data: {
        curindex: 0,
    },
    methods: {
        fnChange(index) {
            this.curindex = index;
        },
    },
    components: {
        //组件1
        "my-top": {
            data() {
                return {
                    titleArr: ["体育", "娱乐", "新闻", "综合"],
                    tempIndex: 0,
                };
            },
            template: `
        <div class="hd">
            <span @click="change(index)" :class="tempIndex==index?'current':''" v-for='(item,index) in titleArr' :key="item">{{item}}</span>
        </div>
        `,
            methods: {
                change(index) {
                    this.tempIndex = index;
                    this.$emit("xxx", this.tempIndex);
                },
            },
        },

        //组件2
        "my-body": {
            props: {
                curindex: {
                    default: 0,
                },
            },
            data() {
                return {
                    bodys: [
                        "我是体育模块",
                        "我是娱乐模块",
                        "我是新闻模块",
                        "我是综合模块",
                    ],
                };
            },
            template: `
        <div class="bd">
            <ul>
                <li :class="curindex==index?'current':''" v-for="(item,index) in bodys" :key="item">{{item}}</li>
            </ul>
        </div>
        `,
        },
    },
});

五、完整代码:

<!DOCTYPE html>
<html>
    <head lang="en">
        <meta charset="UTF-8" />
        <title></title>
        <style>
            * {
                margin: 0;
                padding: 0;
            }

            ul {
                list-style-type: none;
            }

            .box {
                width: 400px;
                height: 300px;
                border: 1px solid #ccc;
                margin: 100px auto;
                overflow: hidden;
            }

            .hd {
                height: 45px;
            }

            .hd span {
                display: inline-block;
                width: 90px;
                background-color: pink;
                line-height: 45px;
                text-align: center;
            }

            .hd span.current {
                background-color: purple;
                color: white;
            }

            .bd li {
                height: 255px;
                background-color: purple;
                display: none;
                color: white;
            }

            .bd li.current {
                display: block;
            }
        </style>
        <script src="./js/vue2.js"></script>
    </head>

    <body>
        <div class="box">
            <my-top @xxx="fnChange"></my-top>
            <my-body :curindex="curindex"></my-body>
        </div>
        <script>
            let vm = new Vue({
                el: ".box",
                data: {
                    curindex: 0,
                },
                methods: {
                    fnChange(index) {
                        this.curindex = index;
                    },
                },
                components: {
                    //组件1
                    "my-top": {
                        data() {
                            return {
                                titleArr: ["体育", "娱乐", "新闻", "综合"],
                                tempIndex: 0,
                            };
                        },
                        template: `
        <div class="hd">
            <span @click="change(index)" :class="tempIndex==index?'current':''" v-for='(item,index) in titleArr' :key="item">{{item}}</span>
            </div>
        `,
                        methods: {
                            change(index) {
                                this.tempIndex = index;
                                this.$emit("xxx", this.tempIndex);
                            },
                        },
                    },

                    //组件2
                    "my-body": {
                        props: {
                            curindex: {
                                default: 0,
                            },
                        },
                        data() {
                            return {
                                bodys: [
                                    "我是体育模块",
                                    "我是娱乐模块",
                                    "我是新闻模块",
                                    "我是综合模块",
                                ],
                            };
                        },
                        template: `
        <div class="bd">
            <ul>
                <li :class="curindex==index?'current':''" v-for="(item,index) in bodys" :key="item">{{item}}</li>
            </ul>
            </div>
        `,
                    },
                },
            });
        </script>
    </body>
</html

标签:index,Vue,background,--,height,color,Tab,tempIndex,组件
From: https://www.cnblogs.com/zhouwying/p/16837253.html

相关文章

  • MongoDB--常用语句
    1、插入单条数据db.students.insertOne({name:'Jane',age:21})2、插入多条数据db.students.insertMany([{name:'Kang',age:22},{name:'Mike',age:19}])3、删除单条数......
  • MySQL--常用语句
    MySQL常用语句1、获取数据库信息1.use数据库名;//选择要操作的Mysql数据库,使用该命令后所有Mysql命令都只针对该数据库。2.showdatabases;//列出MySQL数据库管理......
  • JavaScript--字符串
    一、字符串的概述1、String(字符串)数据类型表示零或多个16位Unicode字符序列。字符串可以使用双引号(")、单引号(')或反引号(`)标示。2、ECMAScript中的字符串是不可变的(im......
  • JavaScript--正则表达式
    一、概述正则表达式(RegularExpression)是一个描述字符模式的对象,用于对字符串进行匹配,一般用在有规律的字符串匹配中;常用于表单验证以及相关的字符串匹配二、声明......
  • JavavScript--ES5和ES6(下)
    一、Map基于set和array之上构建的一个集合1、Map的声明使用new关键字和Map构造函数letm=newMap();通过二维数组声明vararr=[[1,2],[3,4]]varmap=ne......
  • JavaScript--运动
    一、概述运动主要是动画的操作,主要是操作某个document元素的属性变化(位置变化)二、运动主要的原理及步骤原理:使用开启定时器setInterval()/setTimeout()和清除定时器c......
  • 浅谈PHP设计模式的观察者模式
    简介观察者模式是行为型模式的一种,定义了对象间一对多的关系。当对象的状态发生变化时候,依赖于它的对象会得到通知。适用场景类似触发钩子事件,可做消息通知、框架底层......
  • JavaScript--循环语句
    一、循环语句的概念1、循环语句,声明一组要反复执行的命令,直到满足某些条件为止。2、循环包括测试条件以及一个块(通常就是{..})。循环块的每次执行被称为一个迭代。二......
  • JavaScript--原型及模块入门
    面向对象回顾核心概念:万物皆对象(顶层对象Object)抽取行为作为方法抽取名词作为属性俩种构建对象的方式构造函数构建es6的形式classclassPerson{ constructor(){/......
  • JavaScript--条件控制语句
    一、if语句if语句有三种形式:简单的if语句,if-else语句,if-elseif语句。1、语法if语句是使用最频繁的语句之一,语法如下://if语句if(condition){statement1}......