Below I have an input which value I attempt to assign to a variable v. My understanding after reading similar posts is that, when the page is initialized, the value of the input is null and thus cannot be read.
<template>
<div>
<input type="text" id="input"/>
{{ v }}
</div>
</template>
<script>
export default {
data(){
return { v: document.getElementById("input").value }
},
}
</script>
Two questions immediately pop up:
- Why doesn't
v: nullcause an error? Would it not be equivalent to the code above? - What is the correct way to assign the value of the input to a variable?