Rap's response is spot on and demonstrates the utilization of Options API successfully.
In addition, here's another approach utilizing Composition API:
- Begin by defining the
value
variable as a reactive variable using Ref().
Ref has the ability to store any data type.
- To modify the reactive variable, make use of the
.value
property.
<template>
<div id="change" @click="changeValue">{{myValue}}</div>
</template>
<script setup>
const myValue = ref("Old");
function changeValue(){
myValue.value = "new"
}
</script>
Please note: In the provided example, I have changed the variable name from value
to myValue
to prevent confusion with the .value
property.
If you wish to delve deeper into this topic, consider exploring the Reactivity in Vue.