Vue父组件调用子组件事件

方法一:子组件监听父组件发送的方法
方法二:父组件调用子组件方法
子组件中:

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
    }
}
发布了21 篇原创文章 · 获赞 0 · 访问量 2851

猜你喜欢

转载自blog.csdn.net/weixin_39593730/article/details/101675423
今日推荐