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

首先在父组件中写一个click事件:
template里面:

<el-button type="primary" size="small" @click="delete">删 除</el-button>
引入子组件部分:
ref="SelfTable"是子组件在父组件中的名字
<SelfTable ref="SelfTable"/>

methods里面:
在父组件的方法中调用子组件的方法

    delete(){
     this.$refs.SelfTable.deleteService();
     // SelfTable是引入的子组件的名字,deleteService是子组件中的方法
    }

示例:

<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.parentHandleclick("嘿嘿嘿");
      }
    }
  }
</script>
发布了91 篇原创文章 · 获赞 82 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_42893625/article/details/105054443