vue3 ref调用v-for遍历的子组件

vue3 ref调用v-for遍历的子组件

链接: link

ref调用v-for遍历的子组件

父组件

<template>
  <div>
	<Children
      v-for="(item, index) in list"
      :key="item.id"
      :itemData="item"
      :index="index + 1"
      :ref="(el: any) => { creatRef(el, index)}"
      @update="update"
    />
    <button onclick="submit"></button>
  </div>
</template>
<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue';
import Children from './Children.vue';
const listRefs = ref<any>([]),
      list = ref([
      	{id: 1, name: '11'},
      	{id: 2, name: '22'},
      ]);
// 遍历生成多个ref
const creatRef = (el: any, index: number) => {
  if (el) {
    listRefs.value[index] = el;
  }
};
const update = (val: any) => {
	console.log(val);
}
const submit= () => {
	listRefs.value?.forEach((item: any) => {
	  item.todo();
	});
}
onMounted(() => {})
</script>

子组件Children

<template>
  <div>
  	<input v-model="itemData.name" />
  </div>
</template>
<script lang="ts" setup>
import {reactive, watch} from 'vue';
const emit = defineEmits(['update']);
const props = defineProps({
  itemData: {
    type: Object,
    default: () => {},
  },
  index: {
    type: number,
    default: 0,
  },
});
const todo = () => {
	console.log('todo');
	emit('update', {
      index: props.index,
      name: itemData.name,
    });
};
defineExpose({
  todo,
});
watch(
	() => props.itemData,
	(newVal) => {
	 	console.log('newVal=', newVal);
	},
	{ deep: true, immediate: true },
);
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你在 Vue 3 中动态生成了组件,并想调用这些动态生成的组件的方法,你可以使用 `ref` 和 `onMounted`。 首先,在父组件中,你需要使用 `ref` 创建一个数组来存储动态生成的组件的引用。然后,在 `onMounted` 钩函数中,可以访问这些引用并调用组件的方法。 下面是一个示例: ```vue <template> <div> <button @click="addDynamicComponent">添加动态组件</button> <div v-for="componentRef in dynamicComponents" :key="componentRef.id"> <component :is="componentRef.component" ref="componentRef.ref"></component> <button @click="callChildMethod(componentRef.ref)">调用组件方法</button> </div> </div> </template> <script> import { ref, onMounted } from 'vue'; export default { setup() { const dynamicComponents = ref([]); const addDynamicComponent = () => { dynamicComponents.value.push({ component: ChildComponent, ref: ref(null) }); }; const callChildMethod = (ref) => { ref.value.childMethod(); // 调用组件方法 }; onMounted(() => { dynamicComponents.value.forEach((componentRef) => { componentRef.ref.value = componentRef.ref; // 将 ref 绑定到实际的组件上 }); }); return { dynamicComponents, addDynamicComponent, callChildMethod }; } }; </script> ``` 在上面的代码中,我们使用 `ref` 创建了一个名为 `dynamicComponents` 的数组,用于存储动态生成的组件的引用。每次点击 "添加动态组件" 按钮时,我们将组件的构造函数和引用添加到数组中。 在模板中,我们使用 `v-for` 遍历 `dynamicComponents` 数组,并使用 `component` 动态地渲染组件。通过点击 "调用组件方法" 按钮时,我们调用 `callChildMethod` 方法,并将对应的组件引用作为参数传递进去。 在 `onMounted` 钩函数中,我们将实际的组件引用绑定到 `dynamicComponents` 数组中的每个元素的 `ref` 属性上。 通过以上步骤,你就可以在动态生成的组件调用它们的方法了。 希望这可以帮助到你!如果有任何进一步的问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值