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!

WhentheVue.jsVirtualDOMdetectsachange,itupdatestheVirtualDOM,diffsit,andappliesminimalchangestotherealDOM.ThisprocessensureshighperformancebyavoidingunnecessaryDOMmanipulations.

Vue.js' VirtualDOM is both a mirror of the real DOM, and not exactly. 1. Create and update: Vue.js creates a VirtualDOM tree based on component definitions, and updates VirtualDOM first when the state changes. 2. Differences and patching: Comparison of old and new VirtualDOMs through diff operations, and apply only the minimum changes to the real DOM. 3. Efficiency: VirtualDOM allows batch updates, reduces direct DOM operations, and optimizes the rendering process. VirtualDOM is a strategic tool for Vue.js to optimize UI updates.

Vue.js and React each have their own advantages in scalability and maintainability. 1) Vue.js is easy to use and is suitable for small projects. The Composition API improves the maintainability of large projects. 2) React is suitable for large and complex projects, with Hooks and virtual DOM improving performance and maintainability, but the learning curve is steeper.

The future trends and forecasts of Vue.js and React are: 1) Vue.js will be widely used in enterprise-level applications and have made breakthroughs in server-side rendering and static site generation; 2) React will innovate in server components and data acquisition, and further optimize the concurrency model.

Netflix's front-end technology stack is mainly based on React and Redux. 1.React is used to build high-performance single-page applications, and improves code reusability and maintenance through component development. 2. Redux is used for state management to ensure that state changes are predictable and traceable. 3. The toolchain includes Webpack, Babel, Jest and Enzyme to ensure code quality and performance. 4. Performance optimization is achieved through code segmentation, lazy loading and server-side rendering to improve user experience.

Vue.js is a progressive framework suitable for building highly interactive user interfaces. Its core functions include responsive systems, component development and routing management. 1) The responsive system realizes data monitoring through Object.defineProperty or Proxy, and automatically updates the interface. 2) Component development allows the interface to be split into reusable modules. 3) VueRouter supports single-page applications to improve user experience.

The main disadvantages of Vue.js include: 1. The ecosystem is relatively new, and third-party libraries and tools are not as rich as other frameworks; 2. The learning curve becomes steep in complex functions; 3. Community support and resources are not as extensive as React and Angular; 4. Performance problems may be encountered in large applications; 5. Version upgrades and compatibility challenges are greater.

Netflix uses React as its front-end framework. 1.React's component development and virtual DOM mechanism improve performance and development efficiency. 2. Use Webpack and Babel to optimize code construction and deployment. 3. Use code segmentation, server-side rendering and caching strategies for performance optimization.


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

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
