分享

vue.js2.0父组件点击触发子组件方法

 梦想成为的样子 2017-09-19

  1. <body>  
  2.     <div id="counter-event-example">  
  3.       <p>{{ total }}</p>  
  4.       <button-counter v-on:ee="incrementTotal"></button-counter>  
  5.       <button-counter v-on:ee="incrementTotal"></button-counter>  
  6.     </div>  
  7.   
  8.     <script>  
  9.         Vue.component('button-counter', {  
  10.           template: '<button v-on:click="increment">{{ counter }}</button>',  
  11.           data: function () {  
  12.             return {  
  13.               counter: 0  
  14.             }  
  15.           },  
  16.           methods: {  
  17.             increment: function () {  
  18.               this.counter += 1  
  19.               this.$emit('ee', 'cc' )  
  20.             }  
  21.           },  
  22.         })  
  23.         new Vue({  
  24.           el: '#counter-event-example',  
  25.           data: {  
  26.             total: 'arg'  
  27.           },  
  28.           methods: {  
  29.             incrementTotal: function (b) {  
  30.               this.total  = b + '1';  
  31.             }  
  32.           }  
  33.         })  
  34.     </script>  
  35. </body>  

子组件通过$emit触发父组件的事件,$emit后面的参数是向父组件传参,注意,父组件的事件处理函数直接写函数名即可,不要加(),参数直接传递到了父组件的methods的事件处理函数了。

  另外,写一个小拾遗。vue子组件用了定义模板组件功能,然后在父组件里定义一个HTML元素绑定这个子组件后才能在父组件通过这个HTML元素使用。

  再说一个非常方便的v-ref

  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title>Document</title>  
  6.     <script src="vue.js"></script>  
  7. </head>  
  8. <body>  
  9. <div id="parent">   
  10.     <input type="text" name="" id="" v-model="msg" />  
  11.     <input type="button" id="" value="dianji" @click="clickDt" />  
  12.     <user-profile ref="profile"></user-profile>    
  13. </div>    
  14.     
  15. <script>    
  16.     Vue.component('user-profile', {    
  17.         template: '<span>{{ msg }}</span>',    
  18.         data: function () {    
  19.             return {  
  20.                 msg: 123  
  21.             };  
  22.         },    
  23.         methods: {    
  24.             greet: function (msg) {    
  25.                 console.log(msg);    
  26.             }    
  27.         }    
  28.     
  29.     })    
  30. //      var parent = new Vue({el: '#parent'});    
  31. //      var child = parent.$refs.profile;    
  32. //      child.greet();    
  33.     new Vue({  
  34.         el:"#parent",  
  35.         data:{  
  36.             msg:""  
  37.         },  
  38.         methods: {  
  39.                 clickDt(){  
  40.                 this.$refs.profile.greet(this.msg);  
  41.             }  
  42.         }  
  43.     })  
  44. </script>    
  45. </body>  
  46. </html>  
Vue2.0组件间数据传递

Vue1.0组件间传递

  使用$on()监听事件;
  使用$emit()在它上面触发事件;
  使用$dispatch()派发事件,事件沿着父链冒泡;
  使用$broadcast()广播事件,事件向下传导给所有的后代

Vue2.0后$dispatch(),$broadcast()被弃用,见https://cn./v2/guide/migration.html#dispatch-和-broadcast-替换

 

1,父组件向子组件传递场景:Father上一个输入框,根据输入传递到Child组件上。

父组件:

复制代码
<template>
  <div>
    <input type="text" v-model="msg">
    <br>
    //将子控件属性inputValue与父组件msg属性绑定
    <child :inputValue="msg"></child>
  </div>
</template>
<style>

</style>
<script>
  export default{
    data(){
      return {
        msg: '请输入'
      }
    },
    components: {
      child: require('./Child.vue')
    }
  }
</script
复制代码

子组件:

复制代码
<template>
  <div>
    <p>{{inputValue}}</p>
  </div>
</template>
<style>

</style>
<script>
    export default{
        props: {
          inputValue: String
        }
    }
</script>
复制代码

2,子组件向父组件传值场景:子组件上输入框,输入值改变后父组件监听到,弹出弹框

 

 父组件:

复制代码
<template>
  <div>
//message为子控件上绑定的通知;recieveMessage为父组件监听到后的方法
    <child2 v-on:message="recieveMessage"></child2>
  </div>
</template>
<script>
  import {Toast} from 'mint-ui'
  export default{
    components: {
      child2: require('./Child2.vue'),
      Toast
    },
    methods: {
      recieveMessage: function (text) {
        Toast('监听到子组件变化'+text);
      }
    }
  }
</script>
复制代码

子组件:

 

复制代码
<template>
  <div>
    <input type="text" v-model="msg" @change="onInput">
  </div>
</template>
<script>
  export default{
    data(){
      return {
        msg: '请输入值'
      }
    },
    methods: {
      onInput: function () {
        if (this.msg.trim()) {
          this.$emit('message', this.msg);
        }
      }
    }
  }
</script>

 

vue2.0父子组件以及非父子组件如何通信


1.父组件传递数据给子组件

父组件数据如何传递给子组件呢?可以通过props属性来实现

父组件:

<parent>
    <child :child-msg="msg"></child>//这里必须要用 - 代替驼峰
</parent>

data(){
    return {
        msg: [1,2,3]
    };
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

子组件通过props来接收数据: 
方式1:

props: ['childMsg']
  • 1
  • 1

方式2 :

props: {
    childMsg: Array //这样可以指定传入的类型,如果类型不对,会警告
}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

方式3:

props: {
    childMsg: {
        type: Array,
        default: [0,0,0] //这样可以指定默认的值
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

这样呢,就实现了父组件向子组件传递数据.

2.子组件与父组件通信

那么,如果子组件想要改变数据呢?这在vue中是不允许的,因为vue只允许单向数据传递,这时候我们可以通过触发事件来通知父组件改变数据,从而达到改变子组件数据的目的.

子组件:
<template>
    <div @click="up"></div>
</template>

methods: {
    up() {
        this.$emit('upup','hehe'); //主动触发upup方法,'hehe'为向父组件传递的数据
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

父组件:

<div>
    <child @upup="change" :msg="msg"></child> //监听子组件触发的upup事件,然后调用change方法
</div>
methods: {
    change(msg) {
        this.msg = msg;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

3.非父子组件通信

如果2个组件不是父子组件那么如何通信呢?这时可以通过eventHub来实现通信. 
所谓eventHub就是创建一个事件中心,相当于中转站,可以用它来传递事件和接收事件.

let Hub = new Vue(); //创建事件中心
  • 1
  • 1

组件1触发:

<div @click="eve"></div>
methods: {
    eve() {
        Hub.$emit('change','hehe'); //Hub触发事件
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

组件2接收:

<div></div>
created() {
    Hub.$on('change', () => { //Hub接收事件
        this.msg = 'hehe';
    });
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

这样就实现了非父子组件之间的通信了.原理就是把Hub当作一个中转站!




    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多