试题
考点

js语言和框架-vue.js-基本语法-if\show

面5笔5

vue中子组件调用父组件的方法有哪些?

前往“校招VIP”小程序,刷题更快
最新校招难题刷题,快来进刷题群吧
解答

第一种方法是直接在子组件中通过this.$parent.event来调用父组件的方法。
第二种方法是在子组件里用$emit向父组件触发一个事件,父组件监听这个事件就行了。
第三种都可以实现子组件调用父组件的方法。

<template>
<div>
<button @click="childMethod()">点击</button>
</div>
</template>
<script>
export default {
props: {
fatherMethod: {
type: Function,
default: null
}
},
methods: {
childMethod() {
if (this.fatherMethod) {
this.fatherMethod();
}
}
}
};
</script>


评论

Bury

2022-05-24 22:00:00

0 0

旺仔扣扣新

2021-11-02 21:00:00

0 0

加载更多