vue中子组件调用父组件的方法

第一种方法是直接在子组件中通过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>

发表评论

后才能评论