Replies: 3 comments 13 replies
-
If by hook you mean composables - you still can pass a ref as an argument. Why do you need to create it inside a composable? function useFooElement(element) {
// do smth with element
return { foo }
}
const element = useTemplateRef<HTMLElement>('someElement');
const { foo } = useFooElement() instead of function useFooElement() {
const element = ref()
// do smth with element
return { element, foo }
}
const { foo, element } = useFooElement() Then it's more flexible and you control connection with the element. |
Beta Was this translation helpful? Give feedback.
-
In my opinion, it works as expected. const elRef = ref<HTMLElement | null>(null); This template ref is maintained for compatibility, where the variable name corresponds to the element's ref attribute. And the new one requires key to be explicitly passed. const elRef = useTemplateRef<HTMLInputElement>(key); I also don’t think that if a key isn’t provided, the variable name should be used instead. |
Beta Was this translation helpful? Give feedback.
-
Here's an example demonstrating when <script setup lang="ts">
import { ref, useTemplateRef } from 'vue';
const input1 = useTemplateRef('input1')
const input2 = ref()
input1.value
// ^? (property) value: InputElement
input2.value
// ^? (property) value: Ref<any, any>.value: any
</script>
<template>
<input ref="input1" />
<input ref="input2" />
</template> I see two main benefits of using
Additionally, when you move the |
Beta Was this translation helpful? Give feedback.
-
before
new
I now need to pass in this key from the outside to use it, which is a little more cumbersome than before
Beta Was this translation helpful? Give feedback.
All reactions