Home  >  Article  >  Web Front-end  >  What happens in vue.use

What happens in vue.use

藏色散人
藏色散人Original
2021-01-26 09:28:152692browse

vue.use is used to register global plug-ins. It receives a function or an object containing the install attribute as a parameter; if the parameter contains install, install will be executed. If not, the parameter will be executed directly as install; and the first The parameters are always vue objects, and registered plug-ins will not be re-registered.

What happens in vue.use

The operating environment of this tutorial: windows7 system, vue2.0 version, Dell G3 computer.

Related recommendations: "vue.js Tutorial"

Definition

vue.use() injects a plug-in globally , for global real-time use, no need to quote separately

Code understanding:

import Router from 'vue-router'
// 入口文件全局注入vue-router, 从而可以在全局使用this.$route
Vue.use(Router)

If you do not use vue.use, you must introduce it separately if you need to use it in the component

// a.vue
import Router from 'vue-router'
// b.vue
import Router from 'vue-router'

After understanding its basic use and function, let's take a look at what happens in vue.use

The source code is very small, so I directly excerpted it

Vue.use = function (plugin: Function | Object) {
const installedPlugins = (this._installedPlugins || (this._installedPlugins = []))
if (installedPlugins.indexOf(plugin) > -1) {
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
}

Summary

vue.use() is used to register global plug-ins. It receives a function or an object containing the install attribute as a parameter. If the parameter contains install, install will be executed. If not, the parameter will be directly executed as install. The first parameter It is always a vue object, and registered plug-ins will not be re-registered.

The above is the detailed content of What happens in vue.use. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn