Skip to content

Instantly share code, notes, and snippets.

@eladcandroid
Created August 2, 2019 10:29
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 eladcandroid/6b45bad6f209beac0bc7fc4facf0182c to your computer and use it in GitHub Desktop.
Save eladcandroid/6b45bad6f209beac0bc7fc4facf0182c to your computer and use it in GitHub Desktop.
Vue 3 Function API Basic Example
<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