2018-09-25非父子组件的传值

2018-09-25  本文已影响8人  其实_dnhl
<div id="app">
    <boy></boy>
    <girl></girl>
</div>
<script src="js/vue.js"></script>
<script>
    var kfc = new Vue();
    Vue.component("boy",{
        template:`
        <div>
            <h1>我是传出的</h1>
            <button @click="sendmsg">发送数据传</button>
        </div>
        `,
        data:function(){
            return{
                msg:"我是boy组件,要传给girl"
            }
        },
        methods:{
            sendmsg:function(ssw){
                kfc.$emit("send",this.msg)
            }
        }
    });

    Vue.component("girl",{
        template:`
            <div>
                <h1>我是接收的</h1>
                <a href="">{{mess}}</a>
            </div>
        `,
        data:function(){
            return{
                mess:""
            }
        },
        mounted:function(){
            kfc.$on("send",msg=>{
                this.mess = msg
            })
        }
    });

    new Vue({
        el:"#app"
    });
</script>
上一篇 下一篇

猜你喜欢

热点阅读