Created
August 2, 2019 10:29
-
-
Save eladcandroid/6b45bad6f209beac0bc7fc4facf0182c to your computer and use it in GitHub Desktop.
Vue 3 Function API Basic 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> | |
<span>count is {{ count }}</span> | |
<span>plusOne is {{ plusOne }}</span> | |
<button @click="increment">count++</button> | |
</div> | |
</template> | |
<script> | |
import { value, computed, watch, onMounted } from 'vue' | |
export default { | |
setup() { | |
// reactive state | |
const count = value(0) | |
// computed state | |
const plusOne = computed(() => count.value + 1) | |
// method | |
const increment = () => { count.value++ } | |
// watch | |
watch(() => count.value * 2, val => { | |
console.log(`count * 2 is ${val}`) | |
}) | |
// lifecycle | |
onMounted(() => { | |
console.log(`mounted`) | |
}) | |
// expose bindings on render context | |
return { | |
count, | |
plusOne, | |
increment | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment