vue中父组件触发子组件中的方法

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>
  </div>
</template>
 
<script>
  import Child from './child';
  export default {
    name: "parent",
    components: {
      child: Child
    },
    methods: {
      clickParent() {
        this.$refs.mychild.handleClick();
      }
    }
  }
</script>

this.$refs.mychild.handleClick()
其中mychild为子组件的ref名,handleClick为子组件中的方法

猜你喜欢

转载自blog.csdn.net/weixin_43254676/article/details/87976842