Using the Vue 3 composition API, how to return the calculated value of the property firstDigit
? The keyword this
in the computed property is undefined
but when I exclude this
I get the error fourDigits is not Defined
.
<script setup> import { computed, reactive } from 'vue' const input = reactive({ fourDigits: Array(1,2,3,4), firstDigit: computed(() => { return this.fourDigits[0] <===== `this` is undefined but if I leave `this` out, then `fourDigits` is undefined. }) </script> <template> <div> <pre> {{JSON.stringify(input.firstDigit, null, 2)}} </pre> </div> </template>
If I need to use a state property to assign a value to another state property, I can do this in the onMounted() hook. like this:
Check if it works for you. wish all the best!
this
is something else in the composition API, try using: