Mixins – Mixins are used to reuse code in Vue components that perform the same action. Mixins are like functions in C programming. We can define some actions in mixins and use it wherever it is necessary. Code reusability is simple with the help of mixins.
There are two types of mixins in Vue:-
1. Local Mixins – Local mixins only functions when we use the mixin object in our Vue component. Here, we will use local mixins.
Syntax :
const MixinObjectName = {
methods: {
// you can write your methods here
}
}
2. Global Mixins – If we need the same functionality for all components in Vue, we can use global mixins. Global mixins affect every Vue component.
A small example of global mixins:
// Creating global mixin
Vue.mixin({
created: function () {
var example = this.$options.example
if (example) {
console.log(example)
}
}
})
new Vue({
example: 'This is Vue!!'
})
Output: This is Vue!!
Pre-requisite:
Approach: First, we will create a Simple Vue.js app without mixin which shows an alert on Button click.
Vue App:
HTML
<!DOCTYPE html>
< html >
< head >
< title >Mixins</ title >
</ head >
< body >
< div id = "app" >
< h1 style = "text-align:center;" >
{{ message }}
</ h1 >
< my-comp1 style = "text-align:center;" >
</ my-comp1 >< br >
< my-comp2 style = "text-align:center;" >
</ my-comp2 >
</ div >
< script >
// Creating component 1
const myComp1 = {
template:
`< div >< button @ click = "pressed('Button 1 is pressed')" >Button 1</ button > </ div >`,
// Creating method which shows
// alert on button click
methods: {
pressed(val) {
alert(val);
}
}
}
// Creating component 2
const myComp2 = {
template:
`< div >< button @ click = "pressed('Button 2 is pressed')" >Button 2</ button ></ div >`,
// Creating method which shows alert on button click
methods: {
pressed(val) {
alert(val);
}
}
}
// Creating vue app
new Vue({
el: '#app',
data() {
return {
message: 'Geeks for Geeks Vue.js | Mixins !!!'
}
},
// Defining components
components: {
myComp1: myComp1,
myComp2: myComp2
}
});
</ script >
</ body >
</ html >
|
In the above code, Component 1 and Component 2 performs the same function (alert message). We wrote separate functions for both the components. So now we can create mixins and reuse the same function in different components.
Creating Mixin for our App:
const Mixin = {
methods: {
//creating method which shows alert on button click
pressed(val){
alert(val);
}
}
}
In the above code, we created an object for mixins as Mixin and defined our function within it. So now we can use this Mixin object in our components.
Adding mixin to existing code:
HTML
<!DOCTYPE html>
< html >
< head >
< title >Mixins</ title >
</ head >
< body >
< div id = "app" >
< h1 style = "text-align:center;" >
{{ message }}
</ h1 >
< my-comp1 style = "text-align:center;" >
</ my-comp1 >< br />
< my-comp2 style = "text-align:center;" >
</ my-comp2 >
</ div >
< script >
// Creating mixin
const Mixin = {
// Creating method which shows
// alert on button click
methods: {
pressed(val) {
alert(val);
}
}
}
// Creating component 1
const myComp1 = {
template:
`< div >< button @ click = "pressed('Button 1 is pressed')" >Button 1</ button > </ div >`,
// Using mixin in component 1
mixins: [Mixin]
}
// Creating component 2
const myComp2 = {
template:
`< div >< button @ click = "pressed('Button 2 is pressed')" >Button 2</ button ></ div >`,
// Using mixin in component 2
mixins: [Mixin]
}
// Creating vue app
new Vue({
el: '#app',
data() {
return {
message:
'Geeks for Geeks Vue.js | Mixins !!!'
}
},
// Defining components
components: {
myComp1: myComp1,
myComp2: myComp2
}
});
</ script >
</ body >
</ html >
|
Output:
Before Clicking the Button:

Vue App
When Button 1 is clicked.

Button 1 click
When Button 2 is clicked.

Button 2 click
Similar Reads
Vue.js Plugins
Vue.js is a progressive JavaScript framework used for building user interfaces and single-page applications. Vue.js plugins are reusable and shareable code that can enhance or add functionalities to Vue applications. They help developers to extend the capabilities of Vue by adding global methods, di
5 min read
Vue.js | Routing
Routing is one of the many features provided by Vue.js to allow users to switch between pages without refreshing every time a page is loaded. This results in smooth transitions between pages giving a better feel for the user.Setting Up The Application: Firstly, we need to create a project to work on
3 min read
Vue.js Methods
A Vue method is an object associated with the Vue instance. Functions are defined inside the methods object. Methods are useful when you need to perform some action with v-on directive on an element to handle events. Functions defined inside the methods object can be further called for performing ac
2 min read
Less.js Mixins
In this article, we will see the Mixins in LESS, along with understanding their implementation & usage through the example. A CSS preprocessor is a program that generates the CSS code by using the defined preprocessor ruleset & facilitates reducing the overall complexity of CSS code. LESS is
4 min read
Ruby Mixins
Before studying about Ruby Mixins, we should have the knowledge about Object Oriented Concepts. If we don't, go through Object Oriented Concepts in Ruby . When a class can inherit features from more than one parent class, the class is supposed to have multiple inheritance. But Ruby does not support
3 min read
Vue.js Slots
Vue.js slots are one of the powerful features in the slot system, allowing the creation of flexible and reusable components. Slots act as placeholders within a component, enabling dynamic content insertion from the parent component. Vue.js slots are a powerful feature that brings flexibility and reu
3 min read
Vue.js Props
Vue.js Props is an attribute to the configuration of the component. It is used to pass the data to the component to work dynamically. The props configuration defines the type of data it can receive from different components of Vue. Table of Content Props DeclarationProp Passing DetailsOne-Way Data F
4 min read
Vue.js Event Modifiers
Vue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers
3 min read
Node.js vs Vue.js
Node.js: It is a JavaScript runtime environment, which is built on Chrome's V8 JavaScript engine. It is developed by Ryan Dahl who is a Software Engineer working at Google Brain, he also developed Deno JavaScript and TypeScript runtime. Node.js is cross-platform and open-source which executes JavaSc
3 min read
Vue.js List Rendering
Vue.js is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and suppor
4 min read