vuejs
/

Vue Mixins

Last Sync: Today

On this page

10
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

vuejs

Vue Mixins

What are Mixins?

Mixins are a way to reuse component logic across multiple Vue components by merging shared functionality.

Why Use Mixins?

  • Reuse common logic
  • Reduce code duplication
  • Share functionality across components
  • Simplify maintenance

Basic Example

JavaScriptRead-only
1
const myMixin = {
  data() {
    return { count: 0 };
  },
  methods: {
    increment() {
      this.count++;
    }
  }
};

Using Mixin

JavaScriptRead-only
1
export default {
  mixins: [myMixin]
};

Merging Behavior

Vue merges data, methods, and lifecycle hooks from mixins into components.

Lifecycle Hooks in Mixins

JavaScriptRead-only
1
const mixin = {
  mounted() {
    console.log('Mixin mounted');
  }
};

Limitations

  • Name conflicts between mixin and component
  • Hard to trace logic origin
  • Less explicit than composition API

Best Practices

  • Use mixins for simple reusable logic
  • Avoid large or complex mixins
  • Use Composition API for better clarity
  • Document mixin behavior clearly

Common Mistakes

  • Overusing mixins
  • Creating naming conflicts
  • Mixing too many responsibilities
  • Not understanding merge behavior

Conclusion

Mixins provide a way to reuse logic in Vue, but modern Vue encourages using the Composition API for better structure and maintainability.

Try it yourself

export default { mixins: [myMixin] };

Test Your Knowledge

Q1
of 3

Mixins used for?

A
Routing
B
Reuse logic
C
Styling
D
API
Q2
of 3

Problem with mixins?

A
Speed
B
Conflicts
C
Size
D
Routing
Q3
of 3

Modern alternative?

A
Vuex
B
Composition API
C
Router
D
Props

Frequently Asked Questions

What is a mixin?

A reusable piece of logic shared across components.

Are mixins recommended?

They are useful, but Composition API is preferred.

What is merge behavior?

Vue combines mixin and component options.

Previous

vue global state

Next

vue teleport

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.