DEV Community

Cover image for Practical use-cases of Vue.js mixins
Damilola Emmanuel Olowookere
Damilola Emmanuel Olowookere

Posted on

1

Practical use-cases of Vue.js mixins

I love Vue.js, as I guess you do too (yeah, because you want to read this article). I poured this same love into the reading of the official documentation: a love commensurately reciprocated by the Vue docs team.

If you can, by all means possible, you should be using Vue 3 in your new projects

However, for some reasons that are beyond the scope of this post (/coughs/ laziness /coughs/) I somehow almost always consciously skip the mixins section. That weird voice in my head always whisper things like "here goes all these unnecessary framework over-complication stuff"

So, if you are like me, you have been using Vue for a while now, and you absolutely have not used mixins nor considered using it, then these 2 reasons are the advantages I have gained in using mixins in my Vue 2 projects:

1. De-congestion of Single File Components (SFCs)
When I code SFCs, I like to keep the <script> section as lean as possible. I like keeping the cognitive load of parsing my SFCs to the minimum, and mixins help a lot in this regard. Immediate advantages of this practise include:

  • Better code organisation
  • Perfect for filters (for Vue 2)
  • Icing on cake: easier modular development

2. Escape route for better TypeScript support
Well, Vue 3 brings better support for TypeScript. For Vue 2, the most optimal TS experience is gotten by setting up Class-Style Components.

However, with mixins, I can have a .ts mixin file and do all my Safe Coding™️ there without worrying about limited TS support in SFC files.

These above are the strongest reasons I have for using mixins in my Vue 2 projects.

I hope you learned something in this piece, too.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

Best practices for optimal infrastructure performance with Magento

Running a Magento store? Struggling with performance bottlenecks? Join us and get actionable insights and real-world strategies to keep your store fast and reliable.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️