Vue3 動態 refs


vue2的 this.refs[xxxx]寫法在vue3中無法使用,因為獲取組件實例修改成了 const xxxx = ref(null)

動態refs寫法修改成:

// template:
<template v-for=item in list :key="item.id">
    <iframe :ref="(el)=>setItemRef(el, item.id)" :src="item.trueUrl" />
</template>

// js:
const iframeRefs = {}
const setItemRef = (el, key) => {
  if (el) {
    iframeRefs[key] = el
  }
}

// 獲取實例
const iframeKey = xxx const iframe = iframeRefs[iframeKey] iframe.onload = () => { // ... }

 

官方文檔鏈接: https://v3.cn.vuejs.org/guide/migration/array-refs.html#frontmatter-title


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM