new function for template refs in setup functions #427
Replies: 4 comments 7 replies
-
This seems like a fairly straightforward win without any drawbacks - other than a slightly increased api surface (which ideally would be eventually phased out). Would love some feedback from the core team and if there is interest I could drive this forward into an actual PR. What is the best way to get that feedback? Discord? Tagging people? (@yyx990803 @LinusBorg ?) Thanks!! |
Beta Was this translation helpful? Give feedback.
-
In vuejs/core@3ba70e4 they added a function called Well maybe it is too late now, or maybe it's a good opportunity since it's not fully released yet. But I'd like to add an idea that involves using <script setup lang="ts">
import { ref, useTemplateRef } from 'vue';
import MyComp from './MyComp.vue';
// Old way
const myCompRef = ref<InstanceType<typeof MyComp>>();
// New way
const foo = useTemplateRef<InstanceType<typeof MyComp>>('myCompRef');
</script>
<template>
<my-comp ref="myCompRef" />
</template> Could we somehow get rid of the Not sure if it's possible. But if it is, I think that would make the code even more readable: const foo = useTemplateRef<typeof MyComp>('myCompRef'); |
Beta Was this translation helpful? Give feedback.
-
how about |
Beta Was this translation helpful? Give feedback.
-
Closing the discussion here as |
Beta Was this translation helpful? Give feedback.
-
Just a small suggestion to introduce a new function for template refs in order to increase readability in setup functions.
For example, currently to reference a component in our template (for example
<Popup ref="popup" ... />
) we would writeThe ref name in the template must match our variable name exactly, and it is not immediately obvious that this ref does anything special compared to any other reactive ref which could be holding any other data.
Perhaps there are some important under the hood reasons for marrying the general reactivity ref with template refs, but I think it is slightly confusing and could be made more clear.
What does the proposed API look like?
I suggest we just add a new function to distinguish regular refs from template refs and removes the tight coupling of the variable name. For example:
This will be immediately obvious to readers that
myPopup
is referencing something from the template, and it frees the user up to name the variable as they like. It also means we can make some type assumptions about whats there and what type can be set by the user. Perhaps this could even fire off a warning / error if the ref was not found (toggle-able for when this might be expected).The function could be called
templateRef
,getTemplateRef
,useTemplateRef
,defineTemplateRef
, etc... Depends on what the current conventions are with "use" and "define" and if this fits well enough into either...I think ideally we would deprecate the current behaviour (eventually) to reduce confusion and API surface area.
Thoughts?
(As always thanks for all your hard work on driving vue forward! <3)
Beta Was this translation helpful? Give feedback.
All reactions