How to add dynamic ref in vue.js?
Solution 1:
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.
Solution 2:
Using v-bind:ref
or just :ref
.
Follow below a simple example including how to access a dynamic ref
<template>
<div>
<div class="inputs" v-for="input in inputs" :key="input.id">
<input type="text" v-model="input.name" :ref="'name' + input.id">
<button @click="displayRef('name' + input.id)">
Click to see the input ref
</button>
<hr>
<button @click="displayAllRefs">Click to see all refs</button>
</div>
</div>
</template>
And the script:
<script>
export default {
data() {
return {
inputs: [
{ id: Date.now(), name: '', lastName: '' }
]
}
},
methods: {
displayRef(ref) {
console.log(this.$refs[ref]) // <= accessing the dynamic ref
console.log('The value of input is:',this.$refs[ref][0].value) //<= outpouting value of input
},
displayAllRefs() {
console.log(this.$refs)
}
}
}
</script>
Solution 3:
You have dynamic refs and have multiple elements. To target any single node just pass the index within method params
new Vue({
el: '#app',
data: {
data: [{id: 1}, {id: 2}, {id: 3}],
},
methods: {
addNewClass(index) {
this.$refs.element[index].classList.add('custom-class')
}
},
})
<script src="https://npmcdn.com/vue/dist/vue.js"></script>
<div id="app">
<div v-for="(result, index) in data" :key="index">
<div ref='element' @click='addNewClass(index)' class='custom-container'>
</div>
</div>