天天看點

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
    }
}           

複制