首页 > 其他 > 详细

Vue 父组件传递事件给子组件

时间:2020-09-07 11:52:55      阅读:116      评论:0      收藏:0      [点我收藏+]

父组件传递自定义事件给子组件,子组件显示调用的两种方式

1.父组件使用 v-bind(:属性传递)

  • 父组件
<child
  :mockParent="handleParentEvet"
></child>
  • 子组件需接收props
props:{
  mockParent:{
    type: Function
  }
},
methods:{
  handle(){
    this.mockParent(‘param from child‘)
    // 不能使用 this.$emit(‘mockParent‘,‘sssss‘)
  }
}

2.父组件使用 v-on(@传递),子组件调用时使用边界情况

  • 父组件
<child
  @test="parentTest"
  @update="parentUpdate"
></child>
  • 子组件中无需接收props
methods:{
  handle(){
    this.$listeners.test(‘param from child test‘) // OK
    this.$listeners.update(‘param from child update‘) // OK
    this.$emit(‘update‘,‘param from child update‘) // OK
  }
}

Vue 父组件传递事件给子组件

原文:https://www.cnblogs.com/leslie1943/p/13625953.html

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