How to add dynamic ref in vue.js?

Simply use v-bind like :ref="'element' + result.id" or ref="`element${result.id}`".

Check fiddle here.

new Vue({
  el: '#app',
  data: {
    data: [{id: 1}, {id: 2}, {id: 3}],
  },
  mounted() {
    console.log(this.$refs);
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script>

<div id="app">
<div v-for="(result, index) in data" :key="index">
    <input type="text" type="file" :ref="'element' + result.id" />
</div>
</div>

Edited:
Thanks to Vamsi’s edit, I replaced index with result.id
Edited 8/28:
Thanks to grokpot, I added a sample powered by Template literals.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)