Vue | Vue子组件和父组件如何互相调用对方的方法

★ Vue中子组件调用父组件的方法总结

1、子组件中通过“ this.$parent.event ”来调用父组件的方法。

2、子组件用“$emit”向父组件触发一个事件,父组件监听这个事件即可。

3、父组件把方法传入子组件中,在子组件里直接调用这个方法即可。

Vue中子组件调用父组件的方法,这里有三种方法提供参考。

 第一种方法是直接在子组件中通过this.$parent.event调用父组件的方法。

父组件(Parent.vue):

<template>
  <p>
    <child></child>
  </p>
</template>
<script>
  import child from '~/components/dam/child';
  export default {
    components: {
      child
    },
    methods: {
      fatherMethod() {
        console.log('测试');
      }
    }
  };
</script>

子组件(child):

<template>
  <p>
    <button @click="childMethod()">点击</button>
  </p>
</template>
<script>
  export default {
    methods: {
      childMethod() {
        this.$parent.fatherMethod();
      }
    }
  };
</script>

 第二种方法是在子组件里用 $emit()向父组件触发一个事件,父组件监听这个事件就行了

父组件(Parent.vue):

<template>
  <p>
    <child @fatherMethod="fatherMethod"></child>
  </p>
</template>
<script>
  import child from '~/components/dam/child';
  export default {
    components: {
      child
    },
    methods: {
      fatherMethod() {
        console.log('测试');
      }
    }
  };
</script>

子组件(child):

<template>
  <p>
    <button @click="childMethod()">点击</button>
  </p>
</template>
<script>
  export default {
    methods: {
      childMethod() {
       // 子组件通过$emit()触发事件
        this.$emit('fatherMethod'); 
      }
    }
  };
</script>

  第三种是父组件把方法传入子组件中,在子组件里直接调用这个方法。

父组件(Parent.vue):

<template>
  <p>
    <child :fatherMethod="fatherMethod"></child>
  </p>
</template>
<script>
  import child from '~/components/dam/child';
  export default {
    components: {
      child
    },
    methods: {
      fatherMethod() {
        console.log('测试');
      }
    }
  };
</script>

子组件(child):

<template>
  <p>
    <button @click="childMethod()">点击</button>
  </p>
</template>
<script>
  export default {
    props: {
      fatherMethod: {
        type: Function,
        default: null
      }
    },
    methods: {
      childMethod() {
        if (this.fatherMethod) {
          this.fatherMethod();
        }
      }
    }
  };
</script>

★ Vue中父组件调用子组件的方法总结

在Vue中,父组件可以通过几种方式调用子组件的方法:

  1. 使用ref属性引用子组件实例,然后在父组件中通过this.$refs.refName.methodName()调用。

  2. 使用事件派发,子组件通过$emit触发一个事件,父组件监听这个事件来调用子组件的方法。

下面是使用ref的例子:

<!-- 父组件 -->
<template>
  <div>
    <child-component ref="child"></child-component>
    <button @click="callChildMethod">Call Child Method</button>
  </div>
</template>
 
<script>
import ChildComponent from './ChildComponent.vue';
 
export default {
  components: {
    ChildComponent
  },
  methods: {
    callChildMethod() {
      this.$refs.child.childMethod();
    }
  }
};
</script>
<!-- 子组件 -->
<template>
  <div>
    <p>Child Component</p>
  </div>
</template>
 
<script>
export default {
  methods: {
    childMethod() {
      console.log('Child method called');
    }
  }
};
</script>

下面是使用事件派发的例子:

<!-- 父组件 -->
<template>
  <div>
    <child-component @call-child-method="parentMethod"></child-component>
  </div>
</template>
 
<script>
import ChildComponent from './ChildComponent.vue';
 
export default {
  components: {
    ChildComponent
  },
  methods: {
    parentMethod() {
      console.log('Parent method called');
    }
  }
};
</script>
<!-- 子组件 -->
<template>
  <div>
    <p>Child Component</p>
    <button @click="callParentMethod">Call Parent Method</button>
  </div>
</template>
 
<script>
export default {
  methods: {
    callParentMethod() {
      this.$emit('call-child-method');
    }
  }
};
</script>

在这两个例子中,我们展示了如何在父组件中调用子组件的方法。第一个例子使用ref来直接调用,第二个例子通过自定义事件来触发父组件的方法。

Vue 框架中,父子组件之间的方法调用是常见的开发需求。以下是基于 Vue 2 Vue 3 的实现方式,涵盖了父组件调用子组件方法以及子组件调用父组件方法的完整实现。 ### 父组件调用子组件方法 #### Vue 2 实现方式 在 Vue 2 中,父组件通过 `this.$refs` 获取子组件实例,并直接调用方法。具体实现如下: ```vue <!-- 子组件 ChildComponent.vue --> <template> <div class="child"> <button @click="childMethod">子组件</button> </div> </template> <script> export default { methods: { childMethod() { console.log('子组件方法调用'); } } } </script> ``` ```vue <!-- 父组件 ParentComponent.vue --> <template> <div class="parent"> <button @click="handleParentClick">调用子组件方法</button> <ChildComponent ref="childRef" /> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, methods: { handleParentClick() { this.$refs.childRef.childMethod(); // 调用子组件方法 [^3] } } } </script> ``` #### Vue 3 实现方式(使用 `<script setup>` 语法) 在 Vue 3 中,需要使用 `defineExpose` 显式暴露子组件方法,以便父组件通过 `ref` 调用: ```vue <!-- 子组件 ChildComponent.vue --> <template> <div class="container"> <div> <span>父组件触发了 {{ count }} 次子组件方法</span> </div> </div> </template> <script setup> import { ref } from 'vue'; const count = ref(0); const firstEvent = () => { count.value++; }; defineExpose({ firstEvent }); // 暴露方法父组件调用 [^2] </script> ``` ```vue <!-- 父组件 ParentComponent.vue --> <template> <div class="parent"> <button @click="handleParentClick">调用子组件方法</button> <ChildComponent ref="childRef" /> </div> </template> <script setup> import { ref } from 'vue'; import ChildComponent from './ChildComponent.vue'; const childRef = ref(); const handleParentClick = () => { childRef.value.firstEvent(); // 调用子组件方法 [^2] }; </script> ``` ### 子组件调用父组件方法 子组件通常通过 `$emit` 触发事件,由父组件监听并执行相应的方法。 ```vue <!-- 子组件 ChildComponent.vue --> <template> <div class="child"> <button @click="childMethod">子组件按钮</button> </div> </template> <script> export default { emits: ['child-event'], methods: { childMethod() { this.$emit('child-event'); // 触发事件通知父组件 [^1] } } } </script> ``` ```vue <!-- 父组件 ParentComponent.vue --> <template> <div class="parent"> <ChildComponent @child-event="handleChildEvent" /> <p>父组件响应次数:{{ parentCount }}</p> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { parentCount: 0 }; }, methods: { handleChildEvent() { this.parentCount++; console.log('父组件方法子组件触发'); } } } </script> ``` ### 总结 - **Vue 2** 中,父组件通过 `this.$refs` 调用子组件方法子组件通过 `$emit` 通知父组件。 - **Vue 3** 中,父组件通过 `ref` 获取子组件实例,并结合 `defineExpose` 调用方法子组件同样通过 `$emit` 或 `defineEmits` 与父组件通信。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

儒雅的烤地瓜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值