All jobs / Frontend / web developer / Vue

Frontend / web developer interview question · 0–1 years

In a Vue 3 component using script setup, you try to focus an input through a template ref at the top of setup and it is null. Why, and where does the code go?

The strongest answer

setup runs before the component is rendered, so the element does not exist yet. DOM access belongs in onMounted, when the template ref has been filled

Why it works

Exactly — setup is before render; onMounted is the first hook where the DOM and template refs exist.

What a good answer covers

Answers that fall short

Who gets asked this

More Vue questions