Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Vue.use () and install

Shulou Source: shulou.com Published: 2022-06-01 02:58:21 11月13日 Update

This article will explain in detail how to use Vue.use () and install. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Introduction to the usage of Vue.use () and install

In vue's main.js, we often use the Vue.use (xx) method. For example, we introduce elementUI. In main.js, we usually introduce it through the following code:

Why would import ElementUI from 'element-ui'import' element-ui/lib/theme-chalk/index.css'Vue.use (ElementUI) do this?

Official explanation

Install the Vue.js plug-in. If the plug-in is an object, you must provide an install method. If the plug-in is a function, it will be used as an install method.

When the install method is called, Vue is passed in as an argument. What do you mean? The parameter in Vue.use () must be a function function or an Object object, and if it is an object, an install method must be provided in the object. Vue is then passed in as a parameter.

Summary:

If the argument in Vue.use () is a function function, then the argument to the function is the Vue object.

If the parameter in Vue.use () is an Object object, then the object must provide an install method, and the parameter of the install method is Vue.

Why does Vue.use use install question

What is the difference between the Vue.use registration plug-in and the Vue.prototype.xxx mount method, what are the advantages of using Vue.use, and why use Vue.use instead of Vue.prototype.xxx

Analyze / / Vue source file path from source code: src/core/shared/util.jsexport function toArray (list: any) Start?: number): Array {start = start | 0 let I = list.length-start const ret: Array = new Array (I) while (iMub -) {ret [I] = list [I + start]} return ret} / / Vue source file path: src/core/global-api/use.jsimport {toArray} from'.. / util/index'export function initUse (Vue: GlobalAPI) {Vue.use = function (plugin: Function | Object) { Const installedPlugins = (this._installedPlugins | | (this._installedPlugins = [])) if (installedPlugins.indexOf (plugin) >-1) {/ / if the plug-in is already registered Then return this} / / additional parameters const args = toArray (arguments, 1) args.unshift (this) if (typeof plugin.install = 'function') {plugin.install.apply (plugin, args)} else if (typeof plugin =' function') {plugin.apply (null, args)} installedPlugins.push (plugin) return this}} vue is said on the official website.

The install method should solve the problem of preventing the plug-in from registering multiple times; if you use Vue.prototype.xxx to mount it, you need to remount it each time you use it.

This is the end of the article on "how to use Vue.use () and install". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Methods parameters objects plug-ins functions source code articles that is files more paths good practical code advantages content official situation meaning articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information vpn macOS Xiaomi MariaDB