首页 > 其他分享 >vue3 获取遍历的子组件

vue3 获取遍历的子组件

时间:2024-04-18 15:57:37浏览次数:20  
标签:el 遍历 onMounted vue3 组件 childComponents ref

<template>
  <div>
    <!-- 使用v-for遍历数据,并为每个子组件设置一个ref -->
    <ChildComponent
      v-for="(item, index) in items"
      :key="index"
      :ref="el => setChildRef(el, index)"
    />
  </div>
</template>
 
<script setup>
import { ref, onMounted } from 'vue';
import ChildComponent from './ChildComponent.vue';
 
const items = ref([{}, {}, {}]); // 假设有3个子组件实例
const childComponents = ref([]); // 用于存储子组件的数组
 
// 回调函数,用于设置ref
function setChildRef(el, index) {
  if (el) {
    childComponents.value.push(el);
  }
}
 
onMounted(() => {
  // 在组件挂载后,childComponents数组将包含所有子组件的引用
  console.log(childComponents.value);
});
</script>

 

标签:el,遍历,onMounted,vue3,组件,childComponents,ref
From: https://www.cnblogs.com/qinshizhen/p/18143672

相关文章

  • Vue3 + vite 项目自定义一个svg-icon组件
    1.安装vite-plugin-svg-icons插件npmivite-plugin-svg-icons-D2.vite.config.ts中配置importpathfrom"path";import{createSvgIconsPlugin}from"vite-plugin-svg-icons";exportdefaultdefineConfig({plugins:[......createS......
  • vue3+elementplus+axios+router的入门项目总结
    一、使用vite方式创建项目:1、创建空文件夹,用vscode打开空文件夹,终端上运行如下命令$npminitvite-app[项目名]:初始化项目$cd[项目名]:进入项目$npminstall:安装项目依赖$npmrundev:启动项目2、启动项目后会出现访问地址: 3、进入访问地址: 二、引入elementpuls......
  • HarmonyOS NEXT应用开发之Tab组件实现增删Tab标签
    介绍本示例介绍使用了Tab组件实现自定义增删Tab页签的功能。该场景多用于浏览器等场景。效果图预览使用说明:点击新增按钮,新增Tab页面。点击删除按钮,删除Tab页面。实现思路设置Tab组件的barHeight为0,隐藏组件自带的TabBar。Tabs(){...}.barHeight(0)//隐藏tab......
  • Vue3 使用setup语法糖时, 组件name命名
    1.多使用一个<script>标签命名<script>exportdefault{name:'Person'}</script><scriptsetup>...</script>2.使用插件安装插件vite-plugin-vue-setup-extendpnpminstallvite-plugin-vue-setup-extend-D在vite.con......
  • 面试官:来说说vue3是怎么处理内置的v-for、v-model等指令?
    前言最近有粉丝找到我,说被面试官给问懵了。粉丝:面试官上来就问“一个vue文件是如何渲染成浏览器上面的真实DOM?”,当时还挺窃喜这题真简单。就简单说了一下先是编译成render函数、然后根据render函数生成虚拟DOM,最后就是根据虚拟DOM生成真实DOM。按照正常套路面试官接着会问vue......
  • vue父子组件之间传值以及方法调用
           参考:https://blog.csdn.net/xiasohuai/article/details/80663739       https://blog.csdn.net/qq_33718889/article/details/92696145......
  • 界面组件Telerik UI for WPF 2024 Q1新版亮点 - 全新DateRangePicker组件
    TelerikUIforWPF拥有超过100个控件来创建美观、高性能的桌面应用程序,同时还能快速构建企业级办公WPF应用程序。UIforWPF支持MVVM、触摸等,创建的应用程序可靠且结构良好,非常容易维护,其直观的API将无缝地集成VisualStudio工具箱中。本文将介绍界面组件TelerikUIforWPF在今......
  • vue3 ts 建立项目
    1、安装pnpmnpminstallpnpm-g2、安装vitepnpminstallvite-D3、在需要的文件夹下建项目npmcreatevite@latest 打开4、main.ts引入element-plusnpminstallelement-plus--savemain.tsimportElementPlusfrom'element-plus'import*asElementPlusIconsVue......
  • openGauss Prometheus-Exporter组件命令参考
    命令参考reprocessing-exporter的使用帮助详情:gs_dbmindcomponentreprocessing_exporter--helpusage:[-h][--disable-https][--ssl-keyfileSSL_KEYFILE][--ssl-certfileSSL_CERTFILE][--ssl-ca-fileSSL_CA_FILE][--web.listen-addressWEB.LISTEN_ADDRESS][--web......
  • openGauss Prometheus-Exporter组件使用指导
    使用指导用户可通过gs_dbmind命令启动对应的exporter。下面为用户演示一个完整的Prometheus监控平台的搭建过程。通过命令行进行Prometheus和所有exporter的启动:gs_dbmindcomponentdeployment--runprometheus和各个exporter单独的启动方式可以参考后续步骤,如果用户只采......