Vue父元件呼叫子元件事件
阿新 • • 發佈:2019-01-10
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
}
}