A closer look at mixins in Vue.js
Learn what mixins do in the Vue.js framework and how to use them in your code in this article from Impulso. Read it now!

Editorial translation by Impulso.
How efficient your code is depends a lot on the decisions you make as a developer. The right feature can make all the difference, and that’s where the mixin, an integral part of the framework called Vue.js, comes in.
Vue.js is a powerful JavaScript framework for building an SPA (single-page application). Among its many features are mixins, which let you reuse code across components.
Two benefits stand out: you can reuse code and maintain it in one place. That means mixins can make your development work considerably more efficient.
That makes mixins a topic worth exploring for developers. So we’ve put together some essential information you can use in your day-to-day coding. Take a look!
Why use mixins?
Because mixins let you abstract common functionality, you can keep your code DRY (Don’t Repeat Yourself). That means less code overall, which makes your project easier to maintain in the long run.
You can also use features like closures to create private variables and methods that won’t pollute the global scope. Another big benefit is how easy it becomes to share code across projects.
For example, if you’ve created an object that provides improved form validation, you can easily reuse it in any other project without having to recreate it from scratch. Those are some good reasons to use a mixin.
When should you use a mixin?
You can use a mixin when you want to write clean code, reuse it, or isolate some logic. You can also apply a mixin globally, but be careful: it will affect every Vue instance created afterward. Here are a few things to consider before using this option:
- Check whether the logic is self-contained and can be used in several places without depending on the component’s internals;
- Check whether adding a particular feature requires small changes across several components;
- Avoid adding code that depends on component internals. That dependency defeats the purpose of using a mixin in the first place.
How do you create a mixin?
It’s easy. Just create an object with the options you want to share. Here’s a great example from Matheus Ricelly that makes it easier to understand. Take a look:

Step 1: Create nomeMixins.js
First, a folder called mixins was created, using the.js extension, so the code could be reused.

Step 2: import nomeMixins
Next, just import the mixins and declare them in an array to use them in other files. Ricelly continues with the same example:

Steps 3 and 4: Object in data() and the result
Finally, the images below show how adding the computed property saves you from having to write several lines of code.

Is there documentation available?
Yes! Of course, we couldn’t wrap up a topic as important as Vue.js mixins without sharing the official website (with content in Portuguese), where you can learn more about plugins, merging, and much more.
So, can you see how this feature could help you in your day-to-day work? It makes your code more readable and easier to understand, as well as safer, cleaner, and more efficient.
You can’t afford to be careless when it comes to programming, right? So it’s worth keeping up with updates and new methods as they come along.
Here at Impulso, our goal is to attract, nurture, and engage the people who drive development in IT, because we believe that the more they know, the more productive they become.
Now it’s your turn! If you found this helpful, share the article and pass on what you’ve learned so more people can succeed at programming.


