A brief discussion of what Vue.use is?
What the heck is Vue.use? This article will introduce you to Vue.use. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
When we use Vue for project development, we see that many wheels are used through Vue.use
, which feels very high-end.
But what the hell is Vue.use
? Let's take a look and see what happens.
In fact, these wheels can be called plug-ins, and their functional scope is not strictly limited. They generally include the following types:
- Add global methods or attributes. For example:
vue-custom-element
- Add global resources: directives/filters/transitions/components, etc. For example,
vue-touch
- adds some component options through global mixing. For example,
vue-router
- adds
Vue
instance methods by adding them toVue.prototype
. - A library that provides its own API while providing one or more of the functions mentioned above. For example,
vue-router
No matter how big or small, the functions that the plug-in wants to achieve are nothing more than the above. However, this does not prevent us from creating complex plug-ins, but we still hope to provide users with a simple method of use, and he does not need to pay attention to what is done inside the plug-in. Fixed Vue provides the use method, specifically to use the plug-in before new Vue()
.
Whether it is an officially provided plug-in (such as vue-router
, vuex
) or a third-party plug-in (such as ElementUI
, ant
) all adopt this method, but the functions inside the plug-in are different. Of course, there are many other such plug-ins, and awesome-vue has a large collection of plug-ins and libraries contributed by the community.
Next, let’s take a look at how this mysterious use
method is implemented.
Vue.js
Plugins should expose an install
method. The first parameter of this method is the Vue
constructor, and the second parameter is an optional options object used to pass in the plug-in configuration:
MyPlugin.install = function (Vue, options) { // 1. 添加全局方法或属性 Vue.myGlobalMethod = function () { // 逻辑... } // 2. 添加全局资源 Vue.directive('my-directive', { bind (el, binding, vnode, oldVnode) { // 逻辑... } ... }) // 3. 注入组件选项 Vue.mixin({ created: function () { // 逻辑... } ... }) // 4. 添加实例方法 Vue.prototype.$myMethod = function (methodOptions) { // 逻辑... } // 5. 注册全局组件 Vue.component('myButton',{ // ...组件选项 }) }
Vue.use(MyPlugin,{ // ...options })
The inside of a plug-in is probably As shown above, it is actually nothing more than the above-mentioned things, very simple. Next, let’s take a look at a real case ElementUI
:
const components = [ Pagination, Dialog, Autocomplete/* 此处由于篇幅省略若干个组件 */]; const install = function(Vue, opts = {}) { locale.use(opts.locale); locale.i18n(opts.i18n); // 注册全局组件 components.forEach(component => { Vue.component(component.name, component); }); Vue.use(InfiniteScroll); Vue.use(Loading.directive); // 添加实例方法 Vue.prototype.$ELEMENT = { size: opts.size || '', zIndex: opts.zIndex || 2000 }; // 添加实例方法 Vue.prototype.$loading = Loading.service; Vue.prototype.$msgbox = MessageBox; Vue.prototype.$alert = MessageBox.alert; Vue.prototype.$confirm = MessageBox.confirm; Vue.prototype.$prompt = MessageBox.prompt; Vue.prototype.$notify = Notification; Vue.prototype.$message = Message; }; /* istanbul ignore if */ if (typeof window !== 'undefined' && window.Vue) { install(window.Vue); } export default { version: '2.13.0', locale: locale.use, i18n: locale.i18n, install, CollapseTransition, Loading, Pagination, Dialog, Autocomplete, // ...other components };
We can easily find that it is super simple to implement a plug-in yourself, as long as it is exposed to the outside worldinstall
Method is enough, this method will be called when using Vue.use
. So we only need to put the content to be implemented inside install
. The advantage of this is that the methods that the plug-in needs to call at the beginning are encapsulated in install
, which is more streamlined and more scalable.
You may also notice that install
here actually introduces all the components. As a large plug-in library, this may have some performance issues. Students who have used ElementUI
all know that it supports on-demand introduction. In fact, some clues can be found in the above example.
const components = [ Pagination, Dialog, Autocomplete/* 此处由于篇幅省略若干个组件 */]; // ....省去中间内容 export default { version: '2.13.0', locale: locale.use, i18n: locale.i18n, install, CollapseTransition, Loading, Pagination, Dialog, Autocomplete, // ...other components };
Here each component is exported separately, and inside each component, it is similarly exposedinstall
to assemble each component, so that it can be Vue.use
Each component is introduced on demand.
import Alert from './src/main'; /* istanbul ignore next */ Alert.install = function(Vue) { Vue.component(Alert.name, Alert); }; export default Alert;
In addition to the above, there are several points worthy of our attention:
- If the plug-in passes in an object, it will execute its
install
Method, if it is a function, executes itself,bind
this
isnull
, and then passes in additional parameters
if (typeof plugin.install === 'function') { plugin.install.apply(plugin, args); } else if (typeof plugin === 'function') { plugin.apply(null, args); }
- If the plug-in has not been registered, an
installed
attribute will be added to the plug-in after successful registration, with a value oftrue
. TheVue.use
method will internally detect theinstalled
attribute of the plug-in to avoid repeated registration of the plug-in
Vue.use
actually does not Mysterious, the interior is still the same things we usually use, it's just a high-end coat put on them. We can also try to use this method during development, which is not only simple, but also powerful.
Related recommendations:
2020 front-end vue interview questions summary (with answers)
vue tutorial Recommended: The latest 5 vue.js video tutorial selections in 2020
For more programming-related knowledge, please visit:Introduction to Programming! !
The above is the detailed content of A brief discussion of what Vue.use is?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



When using the Vue framework to develop front-end projects, we will deploy multiple environments when deploying. Often the interface domain names called by development, testing and online environments are different. How can we make the distinction? That is using environment variables and patterns.

The difference between componentization and modularization: Modularization is divided from the perspective of code logic; it facilitates code layered development and ensures that the functions of each functional module are consistent. Componentization is planning from the perspective of UI interface; componentization of the front end facilitates the reuse of UI components.

Ace is an embeddable code editor written in JavaScript. It matches the functionality and performance of native editors like Sublime, Vim, and TextMate. It can be easily embedded into any web page and JavaScript application. Ace is maintained as the main editor for the Cloud9 IDE and is the successor to the Mozilla Skywriter (Bespin) project.

Foreword: In the development of vue3, reactive provides a method to implement responsive data. This is a frequently used API in daily development. In this article, the author will explore its internal operating mechanism.

Vue.js has become a very popular framework in front-end development today. As Vue.js continues to evolve, unit testing is becoming more and more important. Today we’ll explore how to write unit tests in Vue.js 3 and provide some best practices and common problems and solutions.

How to handle exceptions in Vue3 dynamic components? The following article will talk about Vue3 dynamic component exception handling methods. I hope it will be helpful to everyone!

In Vue.js, developers can use two different syntaxes to create user interfaces: JSX syntax and template syntax. Both syntaxes have their own advantages and disadvantages. Let’s discuss their differences, advantages and disadvantages.

In the actual development project process, sometimes it is necessary to upload relatively large files, and then the upload will be relatively slow, so the background may require the front-end to upload file slices. It is very simple. For example, 1 A gigabyte file stream is cut into several small file streams, and then the interface is requested to deliver the small file streams respectively.
