组件之间通信

  1. 利用props实现父组件给子组件传递数据,就是子组件可以使用到父组件的数据
<body>
    <div id="box">
        <father></father>
    </div>
    <template id="father">
        <div>
            <p>这是{{msg}}</p>
            <son :message="msg"></son> //把父组件的数据绑定在子组件身上
        </div>
    </template>
    <template id="son">
        <p>{{message+'lll'}}</p>   //在子组件中使用从父组件上拿到的数据
    </template>
</body>
<script src="../base/vue.js"></script>
<script>
    
    Vue.component('father',{
        template:'#father',//父组件
        data:function(){
            return {
                num:20,
                msg:'王思懿'
            }
        },
        components:{
            son:{//子组件
                template:'#son',
                props:['message'],  //取到从父组件身上取到的数据
            }
        }
    })
    new Vue({
        el:'#box'
    })
</script>
  1. 通过$emit实现父子组件通信,让父组件可以使用子组件的数据
<body>
    <div id="box">
        <father></father>
    </div>
</body>

<template id="father">
    <div>
        <p>爸爸在此要说{{newData}}</p>
        <son @change='changeData'></son>  //给子组件绑定父组件的方法
    </div>
</template>

<template id="son">
    <div>
        <p>子组件在此</p>
        <button @click='speak'>说话</button>//给子组件绑定事件
    </div>
</template>
<script src="../base/vue.js"></script>
<script>
//创建父组件
Vue.component('father',{
    template:'#father',
    data:function(){
        return {
            newData:''
        }
    },
    methods:{
        changeData(data){
            this.newData=data
        }
    }
})
//创建子组件
Vue.component('son',{
    template:'#son',
    data:function(){
        return {
            data:'王思懿啦啦啦'
        }
    },
    methods:{
        speak:function(){
            this.$emit('change',this.data)   //调用这个方法,就是找到子组件绑定那个change方法,也就是创建在父组件身上的changeData方法,第二个参数就是changeData的传参
        }
    }
})
new Vue({
    el:'#box'
})
  1. ref实现兄弟之间的通信
<body>
    <div id="box">
        <bigbrother></bigbrother>
        <littlebrother ref='aaa'></littlebrother>
    </div>
    <template id="bigbrother">
        <div>
            <p>{{myData}}</p>
            <button @click='hit'>哥哥打弟弟</button>
        </div>
    </template>
    <template id="littlebrother">
        <div>
            <p>{{myData}}</p>
        </div>
    </template>
</body>
<script src="../base/vue.js"></script>
<script>
    Vue.component('bigbrother',{
        template:'#bigbrother',
        data:function(){
            return {
                myData:'我是大哥'
            }
        },
        methods:{
            hit(){
                // 弟弟的数据被改变
                this.$parent.$refs.aaa.myData='我是弟弟,被打了,呜呜呜'
            }
        }
    })
    Vue.component('littlebrother',{
        template:'#littlebrother',
        data:function(){
            return {
                myData:'我是小弟,我在笑'
            }
        }
    })
    new Vue({
        el:'#box'
    })
</script>
  1. eventbus实现兄弟通信
<body>
    <div id="box">
        <bro></bro>
        <di></di>
    </div>
    <template id="bro">
        <div>
            <p>我是{{name}}</p>
            <button @click='hit'>{{state}}</button>
        </div>
    </template>
    <template id="di">
        <div>
            <p>我是{{diName}}</p>
            <p>我正在{{diState}}</p>
        </div>
    </template>
</body>
<script src="../base/vue.js"></script>
<script>
//创建兄组件
let a=new Vue()
Vue.component('bro',{
    template:'#bro',
    methods:{
        hit(){
            a.$emit('hitdi')
        }
    },
    data(){
        return{
            name:'bro',
            state:'我要打弟弟了'
        }
    }
})
Vue.component('di',{
    template:'#di',
    data(){
        return{
            diName:'wangyanbin',
            diState:'smile'
        }
    },
    methods:{
        changeState(){
            this.diState='cry'
        }
    },
    mounted(){
        a.$on('hitdi',this.changeState)
    }
})
new Vue({
    el:'#box'
})
</script>
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值