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

    xiaoxiao2021-03-25  57

    <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] }; } 123456789 123456789

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

    props: ['childMsg'] 1 1

    方式2 :

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

    方式3:

    props: { childMsg: { type: Array, default: [0,0,0] //这样可以指定默认的值 } } 123456 123456

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

    2.子组件与父组件通信

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

    子组件: <template> <div @click="up"></div> </template> methods: { up() { this.$emit('upup','hehe'); //主动触发upup方法,'hehe'为向父组件传递的数据 } } 12345678910 12345678910

    父组件:

    <div> <child @upup="change" :msg="msg"></child> //监听子组件触发的upup事件,然后调用change方法 </div> methods: { change(msg) { this.msg = msg; } } 12345678 12345678

    3.非父子组件通信

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

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

    组件1触发:

    <div @click="eve"></div> methods: { eve() { Hub.$emit('change','hehe'); //Hub触发事件 } } 123456 123456

    组件2接收:

    <div></div> created() { Hub.$on('change', () => { //Hub接收事件 this.msg = 'hehe'; }); } 123456 123456

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

    顶 1   踩 0

    转载请注明原文地址: https://ju.6miu.com/read-18469.html

    最新回复(0)