Skip to content

Instantly share code, notes, and snippets.

@ABooooo
Created June 19, 2024 11:50
Show Gist options
  • Save ABooooo/ad8c24162d12eab1c6a3cf8060b43d5a to your computer and use it in GitHub Desktop.
Save ABooooo/ad8c24162d12eab1c6a3cf8060b43d5a to your computer and use it in GitHub Desktop.
/*
Computed properties are properties which are usually generated based on reactive data which are cached and only update when dependencies change.
*/
<template>
<p>{{ data.number }}</p>
<p>This number is {{ oddOrEven }}</p>
<button @click="newNumber(5)">Set the number<button/>
</template>
<script setup>
import { reactive, computed } from 'vue'
const data = reactive({
number: 1
})
const newNumber = number => {
data.number = number
}
// when data.number changes, computed property will update
const oddOrEven = computed(() => {
if (data.numbet % 2 === 0) return 'even'
return 'odd'
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment