In this episode, we dive into how reusable logic is handled in Vue 3 using the Composition API, and more specifically, how composables help make our lives easier compared to the old days of mixins. We start by looking at how logic sharing used to work with mixins and the typical export default
in Vue components, and then see how composables offer a more flexible and tidy alternative.
To make things concrete, we walk through the process of creating a custom composable called useCounter
. First, we make a simple helper function in a new helpers
directory that uses ref
to hold a count value and lets us increment it. We show how to import this composable into any component that needs it—demonstrating on the homepage—and how easy it is to share this logic across the app.
After that, we illustrate Vue 3 and Nuxt 3's handy feature of the composables
directory. By moving our composable into a directory called composables
, Nuxt 3 auto-imports it, meaning we don't even need to include messy import statements. This tidies up our code even further and keeps things really modular.
By the end of the video, you’ll have a solid understanding of how to set up and use composables in a Vue 3/Nuxt 3 project, and you’ll see why they’re such a big improvement over mixins for sharing logic!