Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting dynamic $refs value in Vue js 2

I'm working on a vue application. I have dynamic ref binding for input elements that accept quantity Something like this -

:ref="'qty' + index"

When I log this.$refs, I get all the refs. qty0, qty1, qty2 and so on.

My question is, how do I get the value of a particular input element using the ref?

I cannot hardcode this.$refs.qty0 as the index keeps changing.

I tried

let quantityRef = 'qty' + index;
console.log(index); // 0
console.log(quantityRef); // qty0
console.log(this.$refs.quantityRef); // undefined

Thanks in advance

like image 655
Vishwas Avatar asked Jan 26 '18 00:01

Vishwas


1 Answers

Use a string key directly, with brackets:

this.$refs['qty' + index]
like image 55
Joseph Silber Avatar answered Oct 08 '22 23:10

Joseph Silber