在vue2.0中父组件如何触发子组件的自定义方法?



如果我在父组件的button上绑定了click事件,我想当点击button时可以触发子组件(单文件的子组件xx.vue)的某个方法(如fn1),要这样的效果该怎样实现?之前看了vue1的文档实例里面有methods和events这两者有什么区别,为什么在vue2去掉dispatch后我用emit('fn'),如果fn放在events会没有响应,而放在methods里面才会被触发到?

子组件:

[html]  view plain  copy
  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>  


 

[javascript]  view plain  copy
  1. <template>  
  2.     <div>  
  3.     <button v-on:click="incrementCounter">count +1</button>  
  4.     <button v-on:click="showMask">弹窗</button>  
  5.     <input type="text" v-model="msg">  
  6.     <button v-on:click='fdsf'>emit parent</button>  
  7.     <p>{{something}}</p>  
  8.     <button v-on:click="some">click</button>  
  9.     </div>  
  10. </template>  
  11. <script type="text/javascript">  
  12. import { mapGetters, mapActions } from 'vuex'  
  13. var bus = new   
  14. export default {  
  15.    props:['parentmsg'],  
  16.    data(){  
  17.     return {  
  18.       msg:'hello',  
  19.       something:this.parentmsg  
  20.     }  
  21.    },  
  22.    ready(){  
  23.       console.log(window.location)  
  24.    },  
  25.    events:{  
  26.       emitchild(){  
  27.         console.log('ds0')  
  28.       }  
  29.    },  
  30.    methods:{  
  31.     ...mapActions([  
  32.        'incrementCounter',  
  33.        'showMask'  
  34.     ]),  
  35.     fdsf(){  
  36.       this.$emit('clickfn',this.msg);  
  37.     },  
  38.     some(){  
  39.       this.$emit('childjian',this.msg)  
  40.     },  
  41.     childdomeing(){  
  42.       console.log('child99')  
  43.     },  
  44.     emitchild(){  
  45.         console.log('ds0')  
  46.       }  
  47. }  
  48. }  
  49. </script>  


父组件

 

[javascript]  view plain  copy
  1. <template>  
  2.     <div>  
  3.         <Display></Display>  
  4.         <Increment v-bind:parentmsg = "something" v-on:childjian="parentjian" v-on:clickfn = "dosomething" v-on:emitchild ="emitchild"></Increment>  
  5.         <show-mask v-if="showHide">  
  6.             <show-info></show-info>  
  7.         </show-mask>  
  8.         <button v-on:click='emitchild(something)'>emit child</button>  
  9.     </div>  
  10. </template>  
  11. <script type="text/ecmascript-6">  
  12.     import Display from './Display.vue';  
  13.     import Increment from './Increment.vue';  
  14.     import store from '../vuex/store' ;  
  15.     import Mask from './Mask.vue';  
  16.     import Xinjian from './xinjian.vue'  
  17.     export default{  
  18.         components:{  
  19.             Display:Display,  
  20.             Increment:Increment,  
  21.             showMask:Mask,  
  22.             showInfo:Xinjian  
  23.         },          
  24.         data(){  
  25.             return {  
  26.                something:'hello child'  
  27.             }  
  28.         },  
  29.         computed:{  
  30.             showHide(){  
  31.                 return store.state.showMask;  
  32.             }  
  33.         },  
  34.         store:store,  
  35.         events:{  
  36.               
  37.         },  
  38.         methods:{  
  39.             parentjian(msg){  
  40.                 console.log('child click:'+msg)  
  41.             },  
  42.             dosomething(msg){  
  43.                 console.log(10);  
  44.                 if(msg)  
  45.                     console.log(msg)  
  46.                 console.log(this.$children)  
  47.             },  
  48.             emitchild(msg){  
  49.                 console.log(999)  
  50.                 this.$emit('showMask',msg);  
  51.             }  
  52.         }  
  53.     }  
  54. </script>  

Vue 2.0 废弃了 $broadcast 和 $dispatch,不过可以用 $children 访问子组件,或者通过 $refs 访问子组件,然后直接调用子组件的方法。
由于 events 实例选项属性已废弃,因此只能通过 created 钩子实现对自定义事件的监听,使用 this.$on 或者 this.$one。参见:
https://vuejs.org/v2/guide/mi...



组件中的v-on绑定自定义事件理解

每个 Vue 实例都实现了事件接口(Events interface),即:

使用 $on(eventName) 监听事件

使用 $emit(eventName) 触发事件

Vue的事件系统分离自浏览器的EventTarget API。尽管它们的运行类似,但是$on 和 $emit 不是addEventListener 和 dispatchEvent 的别名。

另外,父组件可以在使用子组件的地方直接用 v-on 来监听子组件触发的事件。 
下面是一个例子: 
这里写图片描述



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值