Vue3 + JavaScript 父组件点击按钮触发子组件事件方法

在 Vue 3 中,父组件点击按钮触发子组件事件有以下三种常用方式:

方法 1:使用 ref 直接调用子组件方法(推荐)

vue

复制

下载

<!-- 父组件 -->
<template>
  <button @click="callChildMethod">父组件按钮</button>
  <ChildComponent ref="childRef" />
</template>

<script setup>
import { ref } from 'vue';
import ChildComponent from './ChildComponent.vue';

const childRef = ref(null);

function callChildMethod() {
  if (childRef.value) {
    childRef.value.childMethod(); // 直接调用子组件方法
  }
}
</script>

vue

复制

下载

<!-- 子组件 ChildComponent.vue -->
<script setup>
// 暴露给父组件的方法
const childMethod = () => {
  console.log('子组件方法被调用');
  // 执行子组件逻辑
};

// 暴露方法给父组件
defineExpose({
  childMethod
});
</script>

方法 2:通过 Props 传递回调函数

vue

复制

下载

<!-- 父组件 -->
<template>
  <button @click="triggerChild">父组件按钮</button>
  <ChildComponent :parentCallback="callback" />
</template>

<script setup>
import ChildComponent from './ChildComponent.vue';

const callback = () => {
  console.log('父组件的回调函数被执行');
};

function triggerChild() {
  // 通过触发子组件事件间接执行
  // 实际执行逻辑在子组件内
}
</script>

vue

复制

下载

<!-- 子组件 -->
<template>
  <!-- 接收父组件传递的回调 -->
</template>

<script setup>
const props = defineProps(['parentCallback']);

// 子组件内执行回调
function executeParentCallback() {
  if (props.parentCallback) {
    props.parentCallback();
  }
}

// 暴露方法供父组件调用
defineExpose({ executeParentCallback });
</script>

方法 3:使用自定义事件(子组件触发)

vue

复制

下载

<!-- 父组件 -->
<template>
  <button @click="emitEvent">父组件按钮</button>
  <ChildComponent @child-event="handleEvent" />
</template>

<script setup>
import ChildComponent from './ChildComponent.vue';

function emitEvent() {
  // 触发自定义事件(实际由子组件监听)
}

function handleEvent(data) {
  console.log('收到子组件事件:', data);
}
</script>

vue

复制

下载

<!-- 子组件 -->
<script setup>
const emit = defineEmits(['child-event']);

// 当需要执行时触发事件
function triggerEvent() {
  emit('child-event', { data: '子组件数据' });
}

defineExpose({ triggerEvent });
</script>

推荐方案对比

方法优点适用场景
ref 直接调用直接高效,逻辑清晰父组件直接控制子组件特定操作
Props 回调函数符合单向数据流需要传递数据到父组件的情况
自定义事件符合组件解耦原则子组件主动通知父组件的场景

最佳实践建议

  1. 需要直接控制子组件行为时 → 使用 ref 方法

  2. 需要子组件返回数据时 → 使用 Props 回调

  3. 实现组件解耦时 → 使用自定义事件

根据你的具体场景选择最合适的方式,通常 ref 直调是最直接高效的解决方案。

Vue3中,使用Element Plus构建应用时,如果想在父组件通过点击按钮子组件的`<el-pro-table>`表单中获取数据并传递回父组件,可以遵循以下步骤: 1. **设置数据流**: - 在父组件的data中创建一个对象或者变量来存储需要共享的数据,例如 `selectedData`。 ```javascript // 父组件 template 部分 <template> <el-button @click="fetchData">点击获取数据</el-button> <!-- 子组件引用 --> <child-component :tableData="selectedData"></child-component> </template> <script> export default { data() { return { selectedData: {}, // 初始化一个空的对象用于存放子组件传来的数据 }; }, methods: { fetchData() { this.$refs.childComponent.getData(); // 调用子组件方法获取数据 }, }, }; </script> ``` 2. **在子组件中处理数据**: - 在子组件里,当用户交互触发时,比如`<el-pro-table>`的某项操作,将数据通过props或者自定义事件的方式传给父组件。这里假设有一个名为`getData`的方法: ```vue <!-- 子组件模板部分 --> <template> <el-pro-table ref="childComponent" :data="tableData" @some-event="handleDataTableClick"></el-pro-table> </template> <script> import { defineComponent } from &#39;vue&#39;; export default defineComponent({ props: { tableData: Object, // 接收父组件传递的数据 }, methods: { handleDataTableClick(data) { this.$emit(&#39;update:selectedData&#39;, data); // 使用$emit触发自定义事件更新父组件的数据 }, }, }); </script> ``` 3. **监听子组件事件**: - 在父组件的methods中,监听子组件触发的自定义事件`update:selectedData`,并在回调函数中接收新数据。 ```javascript ... methods: { fetchData() { this.$refs.childComponent.getData(); }, receiveDataFromChild(event) { this.selectedData = event; // 更新父组件的数据 }, }, watch: { selectedData(newData) { console.log(&#39;父组件接收到的新数据:&#39;, newData); }, }, ... ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值