Mixins
Mixins can add methods, properties, and default life cycle methods to components that consume them. In the following example, we are defining a mixin that adds a greet
method and a greeting
field to the component's data
function:
export default { Â Â methods: { Â Â Â Â greet(name) { Â Â Â Â Â Â return `${this.greeting} ${name}` Â Â Â Â } Â Â }, Â Â data() { Â Â Â Â return { Â Â Â Â Â Â greeting: 'Hello' Â Â Â Â } Â Â } }
Mixins allow multiple components' shared functionality to be defined independently. They are used through a mixins
component property, which accepts an array.
In the App.vue
file, we can consume the mixin by setting the component's mixins
 property.
The properties and methods of the mixin are then available in the component (just as they would be if they were defined in the component...