So the vue use global API method in vue.js is there to use plugins designed for vue.js. Vue.js can do a lot by itself, but chances are you are going to want to use at at least a few plugins as late versions of the framework even miss things like an http client. However maybe that is not such a bad thing, with some projects that come to mind I might not need an http client in the framework and as such that would result in unnecessary bloat in the framework. Also when it comes to adding an http client I can choose what I want to add, or make my own http client for the project.
With that said it would seem that vue.js is following a more minimal approach with its design, pulling many features out of the framework itself, and leaving it up to the developer how to go about adding that feature if and when it is needed. This allows for greater flexibility when it comes to choosing what to add, if it is even deeded, rather than just having everything packed together right away regardless if you are even using it or not.
Another option to keep in mind is a vue mixin which is a way to make a collection of vue instance options that can be used across two or more vuejs instances. It is also possible to make a mixin global so that it will be there to work with in all vuejs instances.
Still there are situations in which I might want to have something be a kind of global feature of vuejs itself rather than just something that is part of a special constructor, component, or global mixin. So in this post I will be going over a few simple examples of the vue use global API method, and some basic example of vuejs plug in design.
Here I have a very basic example of a vuejs plugin that can be used with the vue use global method. This plugin just adds a single vue instance method that just returns a string when called.
When we have a plugin then it is just a matter of including the plug in in a page with a script tag. When the plugin is available in can the be added to vue by just calling the vue use method and passing the object that has the install function in it to as the first argument.
Once the plugin is installed I can then use that instance method that it added to the Vue class prototype. So then this is just a silly basic hello world example, but lets look at some more examples that actually do something useful.
One of the features that is not built into vuejs at least when it comes to late 2.x versions is an http client. In this section I will be showing how to make a quick simple http client that just makes get requests using XMLHttpRequest.
So then I can use it to add a method to Vue that can be used to make simple get requests.
Of course when it comes to writing this kind of module there are all kinds of things that can be added to it, such as adding support for more than just get requests, making use of promises rather than callbacks and so forth. It might make better sense to just use an http client that is all ready written, rather than making your own. However the idea here is that an http client is something that should maybe not be baled into a font end framework.
In some projects a simple client like this that just makes get requests might work okay, in other projects maybe not, in any case it is up to you how to go about adding this functionality to a project that involves the use of vuejs in the client system of the project.
So then the Vue use method is how to go about creating a Vuejs plugin for all of vuejs itself. There are a few things that come to mind where I might want to have something be part of vuejs itself. However even then I do not always have to, or should. There are other options such as making what I want to use just part of its own separate thing and just use it in the varous life cycle hooks and methods of my vue components. So maybe a plugin is not always the best option, or at least I can not say that I am bothering with them that much thus far at least. Still I might make use of this feature now and then if I think that doing so might just be the best course of action with something.