Skip to content

Instantly share code, notes, and snippets.

@lmiller1990
Created November 26, 2021 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmiller1990/d0d620db6a95902ebcf771a5f5906e98 to your computer and use it in GitHub Desktop.
Save lmiller1990/d0d620db6a95902ebcf771a5f5906e98 to your computer and use it in GitHub Desktop.
Composables
// 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