Getting the Error Cannot read properties of undefined (reading '$refs') though I'm having a reference in the template. Does it mean I must use Vue mounted hook ?
<div class="input-wrapper">
<input type="text" id="phone" placeholder="(555) 555-5555" ref="input"/>
</div>
<script>
this.$refs.input.addEventListener('input', function () {
// some code
});
</script>
Inside root of
<script>of a Vue component, in both Vue 2 and Vue 3,thisisundefined:See it here.
Vue template refs can only be accessed inside any hook or method happening after the component has been mounted and before it is unmounted.
Which means the earliest you can reference
this.$refsis inside mounted. And the latest is in beforeUnmount. And you can also access them in any hook or method happening between those two moments.Considering you're attempting to add an event listener to a HTMLInputElement, I'd recommend using the v-on directive, which automatically adds the event listener on mount and removes it on unmount.
In your case:
As a side note, you should know that a regular function's
thisdoesn't have access to the component context, unless it's an arrow function:Whereas in any method (e.g: the above
myFn),thisis the component context, which has access to all component members.