首页 > 其他 > 详细

vue组件通信

时间:2019-11-29 11:40:59      阅读:57      评论:0      收藏:0      [点我收藏+]

父与子组件通信:--子组件里面通过props接收父组件传过来的数据

1.父组件调用子组件的时候 绑定动态属性
<v-header :title="title"></v-header>
2、在子组件里面通过 props接收父组件传过来的数据
props:[‘title‘]

props:{
‘title‘:String
}

Header.vue:

<template>
  <div>
    <h2>{{title}}</h2>
    <h2>{{homemsg}}</h2>
    <button @click="run(‘123‘)">执行父组件的方法</button>
    <button @click="getParent()">获取父组件的数据和方法</button>
  </div>
</template>

<script>
    export default {
        name: "Header",
        props:[title,homemsg,run,home],
        methods:{
            getParent(){
                console.log(this.title:,this.title);
                console.log(this.home.title:,this.home.title);
                this.home.run(456)
            }
        }
    }
</script>

<style scoped>

</style>

MyHome.vue:

<template>
    <div>
      <Header :title="title" :homemsg=‘msg‘ :run="run" :home="this"></Header>
    </div>
</template>

<script>
  import Header from "./Header";
    export default {
        name: "MyHome",
        data(){
          return{
              msg:我是一个home组件,
              title:首页
          }
        },
        components:{
            Header
        },
        methods:{
            run(data){
                alert(我是Home组件的run方法+data);
            }
        }
    }
</script>

<style scoped>

</style>

 父组件主动获取子组件的数据和方法:--$refs

1.调用子组件的时候定义一个ref
<v-header ref="header"></v-header>
2.在父组件里面通过
this.$refs.header.属性
this.$refs.header.方法

<template>
    <div>
      <Header ref="header" :title="title" :homemsg=‘msg‘ :run="run" :home="this"></Header>
      <button @click="getChildData()">获取子组件的数据</button>
      <button @click="runChildMethod()">执行子组件的方法</button>
    </div>
</template>
    methods:{
//...
        
            getChildData(){
                alert(this.$refs.header.msg);
            },
            runChildMethod(){
                this.$refs.header.getParent();
            }
        }        

 子组件主动获取父组件的数据和方法:$parent

this.$parent.数据
this.$parent.方法

  <button @click="getParent2()">主动获取父组件的数据和方法</button>
        methods:{
            getParent2(){
                console.log(‘$parent.msg:‘,this.$parent.msg);
                this.$parent.run(‘aaaa‘);
            }
        }

非父子组件传值:$emit,$on

1、新建一个js文件 然后引入vue 实例化vue 最后暴露这个实例
2、在要广播的地方引入刚才定义的实例
3、通过 VueEmit.$emit(‘名称‘,‘数据‘)
4、在接收收数据的地方通过 $om接收广播的数据
VueEmit.$on(‘名称‘,function(){

})

New1.vue

<template>
    <div>
      New组件
      <button @click="emitNew1()">New1发送广播数据</button>
    </div>
</template>

<script>
    import VueEvent from "../model/VueEvent";
    export default {
        name: "New1",
        data(){
            return{
                msg:我是New1组件
            }
        },
        methods:{
            emitNew1(){
                VueEvent.$emit(from-New1,this.msg);
            }
        },
        mounted() {
            VueEvent.$on("from-Home1",function (data) {
                console.log("接受的消息",data)
            });
        }
    }
</script>

<style scoped>

</style>

Home1.vue

<template>
    <div>
      Home1组件
      <button @click="emitHome1()">emitHome1发送广播数据</button>
    </div>
</template>

<script>
    import VueEvent from "../model/VueEvent";
    export default {
        name: "Home1",
        data(){
            return{
                msg:我是Home1组件
            }
        },
        methods:{
            emitHome1(){
                VueEvent.$emit("from-Home1",this.msg)
            }
        },
        mounted() {
            VueEvent.$on("from-New1",function (data) {
                console.log("接受的消息",data)
            });
        }
    }
</script>

<style scoped>

</style>

 

VueEvent.js

/**
 * Vue实例
 */


import Vue from ‘vue‘;

var VueEvent = new Vue();

export default VueEvent;

 

 

 

vue组件通信

原文:https://www.cnblogs.com/fly-book/p/11898574.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!