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

<body>
    <div id="counter-event-example">
      <p>{{ total }}</p>
      <button-counter v-on:ee="incrementTotal"></button-counter>
      <button-counter v-on:ee="incrementTotal"></button-counter>
    </div>

    <script>
        Vue.component('button-counter', {
          template: '<button v-on:click="increment">{{ counter }}</button>',
          data: function () {
            return {
              counter: 0
            }
          },
          methods: {
            increment: function () {
              this.counter += 1
              this.$emit('ee', 'cc' )
            }
          },
        })
        new Vue({
          el: '#counter-event-example',
          data: {
            total: 'arg'
          },
          methods: {
            incrementTotal: function (b) {
              this.total  = b + '1';
            }
          }
        })
    </script>
</body>

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

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

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

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<script src="vue.js"></script>
</head>
<body>
<div id="parent"> 
	<input type="text" name="" id="" v-model="msg" />
	<input type="button" id="" value="dianji" @click="clickDt" />
    <user-profile ref="profile"></user-profile>  
</div>  
  
<script>  
    Vue.component('user-profile', {  
        template: '<span>{{ msg }}</span>',  
        data: function () {  
            return {
               	msg: 123
            };
        },  
        methods: {  
            greet: function (msg) {  
                console.log(msg);  
            }  
        }  
  
    })  
//      var parent = new Vue({el: '#parent'});  
//      var child = parent.$refs.profile;  
//      child.greet();  
    new Vue({
        el:"#parent",
        data:{
        	msg:""
        },
        methods: {
        		clickDt(){
        		this.$refs.profile.greet(this.msg);
        	}
        }
    })
</script>  
</body>
</html>
Vue2.0组件间数据传递

Vue1.0组件间传递

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

Vue2.0后$dispatch(),$broadcast()被弃用,见https://cn.vuejs.org/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当作一个中转站!

1
 
0


  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值