Vue Academy · Lesson

Mixins and Custom Directives

Reuse logic with mixins and build custom directives.

Lesson 1 of 313 steps

Mixins and Custom Directives is a free Vue Academy lesson on CoddyKit. This is lesson 1 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, and your progress syncs across the web and the CoddyKit app. The Vue Academy course includes 3 lessons in total.

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.

Free to start

Learn HTML with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
42
Lessons
156

Frequently Asked Questions

Is the “Mixins and Custom Directives” lesson free?

Yes — any 3 lessons of the Vue Academy learning path, including “Mixins and Custom Directives”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Vue Academy course includes 3 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.

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

  1. Mixins and Custom Directives
  2. Plugin Development
  3. Scalable Folder Structure
← Back to Vue Academy