Vue · Senior · Question 38 of 75

How do you type a template ref to a child component?

const modal = useTemplateRef<InstanceType<typeof Modal>>("modal");
modal.value?.open();

Explanation

The value is null before mount, hence optional chaining. Prefer props/events over calling child methods; exposing methods is for imperative APIs like focus() or open().

Further reading

Problem with a question

Vue · Senior · question 38

Test yourself

Take the Vue · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz