Home > Enterprise >  Assigning Vue/JS element ref to a const with Typescript
Assigning Vue/JS element ref to a const with Typescript

Time:02-28

I have a template with one form element and one button element:

  <button
    type="submit"
    id="ms_sign_in_submit"
    ref="submitButton"
    >
  </button>

I have const called submitButton in the script.

const submitButton = ref<HTMLButtonElement | null>(null);

Somehow the submitButton element (id="ms_sign_in_submit") gets assigned/inherited(?) to submitButton.value, but I don't understand how. submitButton element is never explicitly assigned to submitButton const.

CodePudding user response:

Vue automatically assigns a template ref to the ref with a matching name returned from the setup() hook, like in the following code:

<template>
  <button ref="submitButton">Submit</button>
</template>                
  • Related