One solution to this problem is to use a ref to the input element and then use the $nextTick method to set the focus after the input has been rendered. Here's an example:
<template>
<div>
<select v-if="!showInput">
<!-- options here -->
</select>
<input ref="myInput" v-show="showInput">
<button @click="showInput = true">Show Input</button>
</div>
</template>
<script>
import { ref, nextTick } from 'vue';
export default {
setup() {
const showInput = ref(false);
const myInput = ref(null);
const setFocus = () => {
nextTick(() => {
myInput.value.focus();
});
};
return {
showInput,
myInput,
setFocus,
};
},
};
</script>
In this example, we use a ref to the input element and set it to null initially. When the "Show Input" button is clicked, we set the showInput ref to true, which will show the input element. We also call the setFocus method, which will use the $nextTick method to set the focus on the input element after it has been rendered.
Note that we wrap the focus call in the $nextTick method to ensure that it is executed after the input has been rendered. This is because Vue updates the DOM asynchronously, so we need to wait for the next tick before setting the focus.
Also note that we use v-show instead of v-if to ensure that the input element is rendered even if it is initially hidden. This is because v-if will remove the element from the DOM if it is false, which can cause issues with setting the focus.