Created
November 26, 2021 23:20
-
-
Save lmiller1990/d0d620db6a95902ebcf771a5f5906e98 to your computer and use it in GitHub Desktop.
Composables
This file contains 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
// not composable | |
// just a function or 'business logic' | |
function add (n1, n2) { | |
return n1 + n2 | |
} | |
// composable | |
// "bridges" logic and Vue via reactivity | |
useAdd (_n1 = 0, _n2 = 0) { | |
const n1 = ref(n1) | |
const n2 = ref(n2) | |
const sum = computed(() => { | |
return add(n1.value, n2.value) | |
} | |
return { | |
n1, n2, sum | |
} | |
} | |
// usage | |
<template> | |
Sum is {{ sum }} | |
<input v-model="n1" /> | |
<input v-model="n1" /> | |
</template> | |
<script setup> | |
const { n1, n2, sum } = useAdd(2, 3) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment