Vue父组件调用子组件事件方法

2019-11-16,,,

Vue父组件向子组件传递事件/调用事件

不是传递数据(props)哦,适用于 Vue 2.0

方法一:子组件监听父组件发送的方法

方法二:父组件调用子组件方法

子组件:

export default {
  mounted: function () {
   this.$nextTick(function () {
    this.$on('childMethod', function () {
     console.log('监听成功')
    })
   })
  },
  methods {
    callMethod () {
     console.log('调用成功')
    }
  }
}

父组件:

<child ref="child" @click="click"></child>

export default {
  methods: {
   click () {
   this.$refs.child.$emit('childMethod') // 方法1
   this.$refs.child.callMethod() // 方法2
  },
  components: {
   child: child
  }
}

以上这篇Vue父组件调用子组件事件方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持北冥有鱼。

您可能感兴趣的文章:

  • vue父组件向子组件动态传值的两种方法
  • Vue父组件如何获取子组件中的变量
  • vue父组件触发事件改变子组件的值的方法实例详解
  • vue 父组件调用子组件方法及事件
  • vue 父组件中调用子组件函数的方法
  • vue 父组件通过$refs获取子组件的值和方法详解

《Vue父组件调用子组件事件方法.doc》

下载本文的Word格式文档,以方便收藏与打印。