Mixins and Custom Directives
Reuse logic with mixins and build custom directives.
Mixins and Custom Directives is a free Vue Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Reusing Logic
As apps grow you find the same logic in many components. Vue offers several ways to share it. This lesson covers two: the older mixins approach and powerful custom directives.
What Is a Mixin?
A mixin is an object with component options (data, methods, hooks) that gets merged into any component that uses it. It was the classic way to reuse logic in Vue 2.
export const loggerMixin = {
created() {
console.log("Component created")
},
methods: {
log(msg) { console.log(msg) }
}
}Using a Mixin
Add a mixin to a component through the mixins array. Its options merge with the component own options.
import { loggerMixin } from "./loggerMixin"
export default {
mixins: [loggerMixin],
created() {
this.log("Ready")
}
}Mixin Downsides
Mixins have real problems: it is unclear where a property came from (implicit source), names can collide between mixins, and merge rules get confusing. These pain points led to a better approach.
Composables: The Better Way
In Vue 3, composables (functions using the Composition API) replace mixins. They make sources explicit, avoid name clashes, and compose cleanly. Prefer them for new code.
import { ref } from "vue"
export function useCounter() {
const count = ref(0)
function increment() { count.value++ }
return { count, increment }
}Using a Composable
Call the composable in setup and destructure exactly what you need; the source is obvious and explicit.
import { useCounter } from "./useCounter"
export default {
setup() {
const { count, increment } = useCounter()
return { count, increment }
}
}What Is a Custom Directive?
A custom directive packages low-level DOM behavior you can attach to elements with v-name. It is ideal for direct DOM manipulation like focus, scrolling, or tooltips.
Registering a Global Directive
Register a directive on the app with app.directive. The name is used in templates with a v- prefix.
app.directive("focus", {
mounted(el) {
el.focus()
}
})
// usage: <input v-focus />Directive Hooks
Directives have lifecycle hooks: mounted (element inserted), updated (component updated), beforeUnmount (cleanup), and others, mirroring component lifecycle.
app.directive("color", {
mounted(el, binding) {
el.style.color = binding.value
},
updated(el, binding) {
el.style.color = binding.value
}
})The Binding Object
The second hook argument is the binding. It carries value (the passed value), arg (after a colon), and modifiers (after dots).
// <p v-color:bg.bold="theme">
// binding.value -> theme
// binding.arg -> "bg"
// binding.modifiers -> { bold: true }Local Directives
For directives used in just one component, register them locally with the directives option instead of globally.
export default {
directives: {
focus: {
mounted(el) { el.focus() }
}
}
}Quick Check
Test your knowledge of mixins and directives.
Recap
Mixins merge options into components but suffer from implicit sources and name clashes; prefer composables in Vue 3. Custom directives, registered with app.directive or locally, package DOM behavior using hooks like mounted and a binding object. Next you will package functionality as a reusable plugin.
Frequently asked questions
Is the “Mixins and Custom Directives” lesson free?
Yes — the full text of “Mixins and Custom Directives” is free to read here on the web, and the Vue Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Vue Academy course, upgrade to CoddyKit PRO.
What will I learn in “Mixins and Custom Directives”?
Reuse logic with mixins and build custom directives. You practise Vue Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Vue Academy?
No prior experience is required. Vue Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Mixins and Custom Directives” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Vue Academy lesson?
Yes. Every Vue Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Mixins and Custom Directives
- Plugin Development
- Scalable Folder Structure