In daily development, sometimes we need to inject our own plug-ins into Vue. If each component is introduced and then registered, it will obviously increase the workload of development, so we need to make each Vue have this plug-in function, so we prefer vue. use this Vue API.

Vue.use

Official address: cn.vuejs.org/v2/api/#Vue…

Vue.use it is a function. Accepts a parameter that can be either a function or an object. If it is an object, the install method is required, which is automatically called when Vue is instantiated. Mounting plug-ins can be handled within install. One caveat is that the first argument to Install must be the Vue constructor.

Case sharing

We will now inject a plug-in into each instance object of Vue. One of the functions of the plug-in is to simply print the “front-end world of checkboxes”. Although the function seems simple, you may not know the process. So let’s try it out.

1. Create a new folder under the Vue project called myPlugin. There are two js files, one index.js and one myplugin.js.

2. The code in myplugin.js is as follows:

Meanwhile, the contents of index.js are as follows:

In main.js, the code looks like this:

When you’re done, go back to the console and look at the printed VM instance object.

From the console, we see that our plug-in is mounted under the Vue instance.