Skip to content

Instantly share code, notes, and snippets.

@mfaridzia
Created November 5, 2021 10:34
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 mfaridzia/2d279dbe097a213cc2568538c172b964 to your computer and use it in GitHub Desktop.
Save mfaridzia/2d279dbe097a213cc2568538c172b964 to your computer and use it in GitHub Desktop.
// useCounter.js
import { ref, computed } from 'vue'
const useCounter = () => {
const count = ref(0)
const double = computed(() => count.value * 2)
const increment = () => count.value++
return {
count,
double,
increment
}
}
export default useCounter
// Vue component
<template>
<h1>{{count}}</h1>
<button @click="resetCounter"> Reset </button>
</template>
<script>
import useCounter from './useCounter'
export default {
name: 'App',
setup() {
const { count } = useCounter()
return {
count
}
},
methods: {
resetCounter() {
this.count = 0
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment