封装模板,父子组件通讯

1.创建模板文件,components 下,index下 swiper-tab-head.vue,把首页相关代码复制到swiper-tab-head.vue的template里,css文件也放进去。创建script,把相关用到的比如点击事件相关复制过来

<script>
    export default { 
        props:{
            tabBars:Array,
            tabIndex:Number
        },
        methods:{
            tabtap(index){
                //this.tabIndex = index;
                //子组件传递给父级
                this.$emit('tabtap',index)
            },
        }
    }
</script>
View Code

 

2.定义子组件里面props,把需要传递参数的 2个变量设置好

 

 

3.父组件引入子组件vue文件

import swiperTabHead from "@/components/index/swiper-tab-head.vue";

 

  注册数据

 

在父级template 里面 引入数据

 

 @tabtap=‘tabtap’是用来监听的,

 

4. swiper-tab-head.vue,下的 

methods:{
            tabtap(index){
                //this.tabIndex = index;
                //子组件传递给父级
                this.$emit('tabtap',index)
            },
        }

把index 传递过去给

 

 posted on 2022-05-06 19:44  编程之路任重道远  阅读(199)  评论(0)    收藏  举报