vue mixin and creating gloabl and local custom Vue options
There sure is a lot to cover to get up and running with vuejs to get into a space where a developer can start making some interesting and useful projects. In this post I will be writing about what a vue mixin is, which is one of many little things that one should have a solid grasp on before diving making a complex vuejs project.
A mixin is a way to create functionality that can be used across two or more components. If you do not have at least some background on vuejs components it might be a good idea to read up more on them to while you are at it. Anyway a mixin object can be passed to a mixins options when creating a component, or it can also be made global for all components and vuejs instances in general by using the Vue.mixin static method.
1 - Vue mixin basics
So a vue mixin is a way to go about defining custom Vue constructor options that will be used for two or more vuejs instances or components. Say that you want to have a create method that will fire for all components, or a set of methods that yuo want to have for a number of components, or even all of them, for these kinds of situations it would be a good idea to try making such things a mixin. It is possible to define one or more mixins for a single Vue constructor instance via the mixins option, and the same can be done globally as well via the Vue mixin global method also. So in this section I will be going over a few quick examples of the two general ways to add mixins.
1.1 - Vue mixin option for adding custom options just for a single Vue constructor instance
To add a mixin to a single vue constructor the vue mixin option should be used. When doing so the options that the mixin add will of course only work for that vue instance to which it is given. Say I have a object with a create hook method that will set the value for a mess property of a data object, and that is it. So yes just an object, with a single create hook method. Then say I have a number of vuejs instances, I can then pass this object with the create hook to two, but not all, or the vue instances. When doing so the create hook will fire for the vue instances that have the mixin object with the create hook, but it will not fire for the ones that I do not give it to.
|
|
So then the vue instances to which I give the mixin object to will have the message set to what I have set in the create hook of the mixin. They will also use the template that I put in the hook also to style the message a little different. The vue instance that does not get this hook will just work with what is going on in the vue instance only.
This might just be a simple silly little example, but the basic idea is there. I can have some vue options in an object, and then I can pass the object to one or more vuejs instances, or components, and then what I define in the object will be used for all the vue instances that I give it to. However if I want to define some options for everything that I will want to make use of the global Mixin method.
1.2 - Adding a global Vue mixin for all Vue constructor instances.
Here I have an example that does more or less the same thing as the first basic example that just defines a simple local mixin with the mixin option. However when using the Vue mixin global api method this results in the mixin being available in all additional Vue constructor instances from then on.
|
|
2 - Format money mixin example
One thing that I find myself doing so far with mixins is creating a set of methods that I am going to want to use in templates. I can make such methods part of the methods object of a vue instance, bit as a project grows I end up with a lot of components, so I do not want to copy and past the same code over and over again for each component. I could reference a main methods object for each instance that I make global, but the best way i think is to use a mixin, that is after all what it is there for.
|
|
3 - Conclusion
There are a few vuejs examples that I have made so far, and I do find myself using mixis once in a while. So far I find myself creating global mixins for certain methods that I want to be able to use across all components that I am suing in a project. For example say I have this methods that will take a number as an argument and return a string that is formated in a way that is better for presenting the value in a view. Say that I want to use this methods in my templates, all of them not just the main vue instance. One way to go about having a global format money method would be to create global mixin with such a method in the vue methods object.