<template> <input ref="input" /> </template>
<script setup>
import { ref, onMounted } from 'vue'
const input = ref(null)
onMounted(() => {input.value.focus()})
</script>
<template>
<input ref="input">
</template>
<script>
export default {
methods: {
// used to put the focus on this field from the parent
focus() {
this.$refs.input.focus();
}
}
};
</script>
<template>
<div v-for="(item, i) in list" :ref="el => { divs[i] = el }">
{{ item }}
</div>
</template>
<script>
import {
onBeforeUpdate,
reactive,
ref,
} from 'vue';
export default {
setup() {
const list = reactive([1, 2, 3]);
const divs = ref([]);
// Make sure to reset the refs before each update.
onBeforeUpdate(() => {
divs.value = [];
});
return {
list,
divs,
};
},
};
</script>
Vue ref element