Created
June 19, 2024 11:50
-
-
Save ABooooo/ad8c24162d12eab1c6a3cf8060b43d5a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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