Created
November 5, 2021 10:34
-
-
Save mfaridzia/2d279dbe097a213cc2568538c172b964 to your computer and use it in GitHub Desktop.
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
// 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