当前位置: 首页 > Web前端 > JavaScript

vue3+ts(typescript)ref获取单个-多个dom元素

时间:2023-03-26 21:04:21 JavaScript

个人网站https://kuangyx.cntemplate

setup//获取单个domconstinputRef=ref(null);//获取多个domconstarr=ref([]);constdivs=(el:HTMLElement)=>{//声明为HTMLElement类型的数组(arr.valueasArray).push(el);//编译器在这样写时会抛出错误//-->类型“HTMLElement”的参数不可分配给类型“never”的参数。//arr.value.push(el);};onMounted(()=>{//加载后,获取输入焦点inputRef.value&&inputRef.value.focus();//打印多个refDOMconsole.log(arr);});返回{inputRef,div,};