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

本文详细介绍在Vue.js中实现父子组件间通信的三种方法:通过this.$parent调用父组件方法,使用this.$emit触发事件通知父组件,以及将父组件方法作为属性传递给子组件直接调用。
第一种在子组件中通过this.$parent.event来调用;

父组件:

<template>
  <div>
    <testchild></testchild>
  </div>
</template>
<script>
  import testchild from "./testchild";
  export default {
    components: {
      testchild
    },
    methods: {
      fatherFun(obj) {
        alert('我是父组件的方法'+obj);
      }
    }
  };
</script>

子组件:

<template>
  <div>
    <button @click="childFun('1111')">点击</button>
  </div>
</template>
<script>
  export default {
    methods: {
      childFun(obj) {
        this.$parent.fatherFun(obj);//调用父组件中的方法
      }
    }
  };
</script>

第二种是在子组件中用this.$emit

父组件:

<template>
  <div>
    <testchild @fatherFun="fatherFun"></testchild>
  </div>
</template>
<script>
  import testchild from './testchild';
  export default {
    components: {
      testchild
    },
    methods: {
      fatherFun(obj) {//带参数形式
        console.log('测试');
        console.log(obj)
      }
    }
  };
</script>

子组件:

<template>
  <div>
    <button @click="childFun()">点击</button>
  </div>
</template>
<script>
  export default {
    methods: {
      childFun() {
        this.$emit('fatherFun','1');
      }
    }
  };
</script>

第三种是把父组件的方法传到子组件中,在子组件中直接调用

父组件:

<template>
  <div>
    <testchild :fatherFun="fatherFun"></testchild>
  </div>
</template>
<script>
  import testchild from './testchild';
  export default {
    components: {
      testchild
    },
    methods: {
      fatherFun(obj) {
        console.log('测试111');
        console.log(obj)
      }
    }
  };
</script>

子组件:

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

以上三种方法,亲测好用!

Vue 中,子组件调用父组件方法通常是通过 **自定义事件(Custom Events)** 实现的。父组件通过 `v-on` 监听子组件触发的事件,然后执行相应的处理函数。 下面是一个完整的示例,演示子组件如何向父组件通信并触发方法。 --- ### ✅ 示例:子组件调用父组件方法 #### 父组件:`ParentComponent.vue` ```vue <template> <div> <h2>父组件</h2> <ChildComponent @child-event="handleChildEvent" /> <p>来自子组件的消息:{{ messageFromChild }}</p> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { messageFromChild: '' }; }, methods: { handleChildEvent(payload) { this.messageFromChild = payload; } } }; </script> ``` --- #### 子组件:`ChildComponent.vue` ```vue <template> <div> <h3>子组件</h3> <button @click="sendMessageToParent">点击发送消息给父组件</button> </div> </template> <script> export default { methods: { sendMessageToParent() { const message = '你好,父组件!'; this.$emit('child-event', message); // 触发事件并传递数据 } } }; </script> ``` --- ### 🔍 解释 - 子组件通过 `$emit` 方法触发一个自定义事件(如 `'child-event'`),并可以传递参数。 - 父组件使用 `v-on:child-event` 或简写为 `@child-event` 来监听这个事件。 - 当事件被触发时,父组件中对应的处理函数(如 `handleChildEvent`)就会被调用,并接收到子组件传来的数据。 --- ### ✅ 附加说明 - 你可以传递任意类型的数据作为参数,比如字符串、数字、对象、数组等。 - 如果你需要从子组件修改父组件的状态,推荐使用 `v-model` 或 `.sync` 修饰符来实现双向绑定(Vue 2),在 Vue 3 中更推荐使用 `v-model` 或组合式 API 的 `defineModel()`。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值