Created
June 11, 2024 23:59
-
-
Save guiliredu/39860607cb0c3de3f4f5263ba6975eb8 to your computer and use it in GitHub Desktop.
Vue 3 Composition API example
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
<template> | |
<div | |
@click="$emit('created')" | |
> | |
</div> | |
</template> | |
<script setup> | |
// Imports from VUE | |
import { onMounted, ref, defineProps, defineEmits, computed } from 'vue'; | |
// Imports from VUE-ROUTER | |
import { useRouter, useRoute } from 'vue-router' | |
// PROPS | |
const props = defineProps(['total']) | |
// EMITS | |
const emit = defineEmits(['created']) | |
// Router | |
const router = useRouter(); | |
const route = useRoute(); | |
const id = route.params.id | |
const slug = route.query.slug | |
// Computed Property | |
const total = computed(() => { | |
return item.value.count; | |
}); | |
// Reactivity State | |
const item = ref({ | |
count: 0, | |
}); | |
// Methods | |
const list = () => { | |
axios.get(`total`) | |
.then(response => { | |
item.value.count = response.data; | |
// Emits event | |
emit('created') | |
}) | |
} | |
// Lifecycle Hooks | |
onMounted(() => { | |
list(); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment