父組件
<template>
<div>
<div @click="click">點擊父組件</div>
<child ref="child"></child>
</div>
</template>
<script>
import child from "./child";
export default {
methods: {
click() {
this.$refs.child.$emit('childMethod','發送給方法一的數據') // 方法1:觸發監聽事件
this.$refs.child.callMethod() // 方法2:直接調用
},
},
components: {
child,
}
}
</script>
子組件
<template>
<div>子組件</div>
</template>
<script>
export default {
mounted() {
this.monitoring() // 注冊監聽事件
},
methods: {
monitoring() { // 監聽事件
this.$on('childMethod', (res) => {
console.log('方法1:觸發監聽事件監聽成功')
console.log(res)
})
},
callMethod() {
console.log('方法2:直接調用調用成功')
},
}
}
</script>
