Skip to content

Instantly share code, notes, and snippets.

@petervmeijgaard
Last active July 15, 2021 19:08
Show Gist options
  • Save petervmeijgaard/8c4481d3e59f0102dcdc35edd9ce6be3 to your computer and use it in GitHub Desktop.
Save petervmeijgaard/8c4481d3e59f0102dcdc35edd9ce6be3 to your computer and use it in GitHub Desktop.
Writing Reusable Vue Composables - Introduction - Composition API
<template>
<div>
<p>My Number - {{ myNumber }}</p>
<p>Parent Number - {{ parentNumber}} - {{ parentNumberPlusOne }}</p>
<button @click="addNumber()">
Add
</button>
</div>
</template>
<script>
import { computed, ref } from 'vue';
export default {
name: 'AsCompositionApi',
props: {
parentNumber: {
type: Number,
required: true,
},
},
setup(props) {
const myNumber = ref(1);
const addNumber = () => {
myNumber.value += 1;
};
const myNumberPlusOne = computed(() => props.parentNumber.value + 1);
return {
myNumber,
addNumber,
myNumberPlusOne,
};
},
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment