A useful introduction to Mixins in Vue, why they are important and how to use them in your workflow.
Mixins in Vue JS are basically a chunk of defined logic, stored in a particular prescribed way by Vue, which can be re-used over and over to add functionality to your Vue instances and components. So Vue mixins can be shared between multiple Vue components without the need to repeat code blocks. Anyone who has used the CSS pre-processor called SASS has a good idea of mixins.
This post is suited for intermediate frontend developers that use Vue JS, and so being conversant with beginner concepts and installation processes is assumed. Here are a few prerequisites you should already have before you start to use Vue CLI 3 through this article.
You will need:
npm uninstall -g vue-cli
then install the new one:
npm install -g @vue/cli
OR
npm install
Mixins are a flexible way to distribute reusable functionalities for Vue components — Official Docs## The problem mixins help to solve
One way to fully understand the importance of mixins in Vue is to see the re-use problem in action. If you have two components that contains a method that does exactly the same thing or performs the exact functionality in the two components like the simple example below:
Navigate into the project folder and to the src folder and create a components directory where you can then create two components: Test.vue and Modal.vue. Copy the code below to the appropriate components respectively.
// Component 1
// src/components/Test.vue
<template>
<div>
<button v-on:click="clicked('you just clicked on button 1')">
Button 1
</button>
</div>
</template>;
export default {
name: "Test",
methods: {
clicked(value) {
alert(value);
}
}
};
The component above displays a button that shows an alert modal when clicked. The second component below does exactly the same thing:
// Component 2
// src/components/Modal.vue
<template>
<div>
<button v-on:click="clicked('you just clicked on button 2')">
Button 2
</button>
</div>
</template>;
export default {
name: "Modal",
methods: {
clicked(value) {
alert(value);
}
}
};
Your App.vue file should have the both components imported and declared exactly like it is below:
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<Test />
<modal />
</div>
</template>;
<script>
import Test from "./components/Test.vue";
import Modal from "./components/Modal.vue";
export default {
name: "app",
components: {
Test,
Modal
}
};
</script>
So you can clearly see that we are repeating the click method code block in the both components and this is not very ideal as it is not an efficient way to handle memory resources and it also goes against the DRY principle.
The team at Vue has now introduced the mixins as a good solution to this problem, with mixins you can encapsulate a piece of code or functionality and then import it for use as often as you want in various components.
Vue Mixin from definition to usage looks like this:
// define a mixin object
var myMixin = {
created: function() {
this.hello();
},
methods: {
hello: function() {
console.log("hello from mixin!");
}
}
};
// define a component that uses this mixin
var Component = Vue.extend({
mixins: [myMixin]
});
var component = new Component(); // => "hello from mixin!"
You are going to use Vue mixins to rebuild the two components we initially used at the beginning of this post to illustrate the problem. To use mixins in a Vue application, you have to pass through four stages:
A mixin file is an exportable javascript file in which the block of code or functionality to be imported and re-used in your various desired Vue components is defined. For developers like myself who like to keep things very modular, create a Mixins folder inside the src folder and then create a clickMixin.js file inside it. Copy the code below into the newly created file.
// src/mixins/clickMixin.js
export default {
methods: {
clicked(value) {
alert(value);
}
}
};
This is the the mixin file, inside is a simple click method that pops up an alert modal. It can be any logic at all, it can have data options, computed properties and anything a Vue component can have. The possibilities are really only limited to your imagination and use case as a developer.
Now that a mixin has been created, the next step is to inject it inside the components where it is needed: where the functionality should come to play. In our demonstration above, that would be inside the two components we already created in the beginning of this post. Import the clickMixin inside the two components with the command below:
import clickMixin from ‘../Mixins/clickMixin’
After importing the mixin, you have to remove the logic you had initially as it is now being taken care of by the mixin. In our case, this means that you will delete the method creation logic in the both components.
// remove this code block and the comma before it.
methods: {
clicked(value){
alert(value);
}
}
This is where you tell the Vue application that what you imported is a mixin and so it should treat the logic inside it as one and do the dirty work of ensuring the application fixes the functionality and the method calls at the appropriate places in the component. Mixins in Vue by default are registered as arrays like it is below:
<script>
import clickMixin from '../Mixins/clickMixin'
export default {
name: 'Test',
mixins: [clickMixin]
}
</script>
If you followed through from the start, your application components should be like it is below:
Test.vue
<template>
<div>
<button v-on:click="clicked('you just clicked on button 1')">
Button 1
</button>
</div>
</template>;
<script>
import clickMixin from "../Mixins/clickMixin";
export default {
name: "Test",
mixins: [clickMixin]
};
</script>
Modal.vue
<template>
<div>
<button v-on:click="clicked('you just clicked on button 2')">
Button 2
</button>
</div>
</template>;
<script>
import clickMixin from "../Mixins/clickMixin";
export default {
name: "Modal",
mixins: [clickMixin]
};
</script>
App.vue
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png" />
<Test msg="Welcome to Your Vue.js App" />
<modal />
</div>
</template>;
<script>
import Test from "./components/Test.vue";
import Modal from "./components/Modal.vue";
export default {
name: "app",
components: {
Test,
Modal
}
};
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
button {
background-color: #10776e; /* Green */
border: none;
margin: 5px;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}
</style>
There are two types of mixins in Vue:
In the hierarchy of things in a Vue application, inside a component mixins are applied first by default. The component is applied second, so that it can override the mixin in any case. So it is important to always remember that when there is a kind of conflict of authority, the Vue component will always have the final say and overriding powers.
You have been introduced to the concept of mixins in Vue, the types and how they are used with a sample demonstration. It is important you also remember to stick to local mixins and only use global mixins in rare cases when you really need it. Happy coding!
#javascript #vue-js #web-development