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

子组件:

<template>
  <div>
    child
  </div>
</template>
 
<script>
  export default {
    name: "child",
    props: "someprops",
    methods: {
      parentHandleclick(e) { //父组件要调用的方法
        console.log(e)
      }
    }
  }
</script>

父组件:

<template>
  <div>
    <button @click="clickParent">点击</button>
    <child ref="mychild"></child>  <!-- **设置一个ref** -->
  </div>
</template>
 
<script>
  import Child from './child'; 
  export default {
    name: "parent",
    components: {
      child: Child
    },
    methods: {
      clickParent() {
        this.$refs.mychild.parentHandleclick("调用成功");
      }
    }
  }
</script>

注:

1、在父组件中:首先要引入子组件 import Child from './child';
2、 <child ref="mychild"></child>是在父组件中为子组件添加一个占位,ref="mychild"是子组件在父组件中的名字
3、父组件中 components: {  是声明子组件在父组件中的名字
4、在父组件的方法中调用子组件的方法,很重要 this.$refs.mychild.parentHandleclick("调用成功");

原文地址:https://www.cnblogs.com/ssjd/p/14469466.html