Vue是支持插件的,而且它的插件机制很简单。就是用Vue.use方法,接收一个函数。接收的这个函数就是用来插件,它能做的事情无非就是在Vue或Vue.prototoye上添加一些方法啥的。让你在Vue上或Vue实例上可以调用你插件提供的功能。
在使用use注册插件的时候就会调用你传递过来的函数,这个函数接收的参数是你调用use时传递来的除了这个函数之外的所有参数,同时还会把Vue放到第一个参数的位置。就是const args = toArray(arguments, 1)
args.unshift(this)这两句。
在core/global-api/use.js
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
}