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

<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>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容