2

In document, it gives code:

<div v-for="item in list" :ref="setItemRef"></div>
export default {
  setup() {
    let itemRefs = []
    const setItemRef = el => {
      if (el) {
        itemRefs.push(el)
      }
    }
    onBeforeUpdate(() => {
      itemRefs = []
    })
    onUpdated(() => {
      console.log(itemRefs)
    })
    return {
      setItemRef
    }
  }
}

But I also want to pass the v-for index into handler, just like this:

// fake code
 <div v-for="(item, index) in navItems" :key="index":ref="setNavItemRefs($el, index)">
   <span>{{ item }}</span>
 </div>

How can I bind this index?

1 Answer 1

5

Try out to define an inline handler like :

<div v-for="item in list" :ref="(el)=>setItemRef(el,index)"></div>

and

 const setItemRef = (el,index) => {
      if (el) {
        itemRefs.push(el)
      }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

in search for a similar solution and tried this but got a compilation error on the lambda from vite, would you perhaps know if this construct is possible in vue3 combined with vite?
could you share with me some code in codesanbox?
seems to have been some temporary issue with vite or volar, no error with the latest version

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.