php怎么建立网站,重庆网站怎么做出来的,桂林百姓生活网,dede页码的调用 网站前言#xff1a;vue2可通过ref来获取当前的dom#xff0c;但是vue3有个问题#xff0c;就是必须定义ref的变量名#xff0c;才能使用#xff1b;倘若有多个ref#xff0c;一个个去定义未免过于繁琐#xff0c;还有一种情况就是dom是使用v-for循环出来的#xff0c;那么… 前言vue2可通过ref来获取当前的dom但是vue3有个问题就是必须定义ref的变量名才能使用倘若有多个ref一个个去定义未免过于繁琐还有一种情况就是dom是使用v-for循环出来的那么ref也就不确定了无法提前定义。 解决方法1:
这是使用v-for循环出来的domref通过index下标来命名
divv-for(item, index) in dataList:keyitem.id
mine-info:refel getMineRef(el, index):titleitem.title:dataitem.data/mine-info
/div此时mineRefList里面放的就是所有ref
const mineRefList refHTMLElement[]([]);
const getMineRef (el:any, index:number) {if (el) {mineRefList .value[index] el; }
};使用forEach循环去取就行这里的 item 就是通过ref拿到的 dom元素。可以操作上面定义的变量或方法
mineRefList.value?.forEach((item: any) {console.log(item)
});解决方法2 注意与上面略相似但是用push可能会造成ref还没渲染完得到null的情况所以最好还是上面那样写 divv-for(item, index) in dataList:keyitem.id
mine-info:refgetMineRef:titleitem.title:dataitem.data/mine-info
/divlet mineRefList refHTMLElement[]([]);
const getMineRef (el:any) {if (el) {mineRefList.value.push(el);}
};mineRefList.value?.forEach((item: any) {console.log(item)
});