插件的本质:

  • 对象;
  • 对象中必须包含 install()

插件

  1. 功能:用于增强Vue

  2. 本质:包含install方法的一个对象,install的第一个参数是Vue,第二个以后的参数是插件使用者传递的数据。

  3. 定义插件:

     对象.install = function (Vue, options) {
         // 1. 添加全局过滤器
         Vue.filter(....)
    
         // 2. 添加全局指令
         Vue.directive(....)
    
         // 3. 配置全局混入(合)
         Vue.mixin(....)
    
         // 4. 添加实例方法
         Vue.prototype.$myMethod = function () {...}
         Vue.prototype.$myProperty = xxxx
     }