天天看点

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

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

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

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

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

子组件:

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

复制

父组件:

ref="child" @click="click">

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

复制