usage

Vue.use( plugin )
Copy the code

Install the plug-in. If the plug-in is an object, you must provide an install method. If the plug-in were a function, it would be used as the install method. When the install method is called, Vue is passed in as an argument; If the install method is called multiple times by the same plug-in, the plug-in will only be installed once

// s-pdf/index.js
import SPdf from './src/s-pdf';

SPdf.install = function(Vue) {
  Vue.component(SPdf.name, SPdf);
};

export default SPdf;

// main.js
import SPdf from 's-pdf'
Vue.use(SPdf)
Copy the code

The source code to achieve

export function initUse (Vue) {
    Vue.use = function (plugin){
        const installedPlugins = (this._installedPlugins || (this._installedPlugins = []))
        if (installedPlugins.indexOf(plugin) > -1 ) { // There are direct returns
            return this
        }
        const args = toArray(arguments.1)
        args = unshift(this)
        if (typeof plugin.install === 'function') {// Pass in the object
            plugin.install.apply(plugin, args)
        } else if (typeof plugin === 'function') {// Pass in the function
            plugin.apply(null, args)
        }
        installedPlugins.push(plugin)
        return this}}// toArray
export function toArray (list, start) {
  start = start || 0
  let i = list.length - start
  const ret = new Array(i)
  while (i--) {
    ret[i] = list[i + start]
  }
  return ret
}
Copy the code