vue-添加全局擴展方法


  • 1.添加全局方法或者屬性,如: vue-custom-element
  • 2.添加全局資源:指令/過濾器/過渡等,如 vue-touch
  • 3.通過全局 mixin 方法添加一些組件選項,如: vue-router
  • 4.添加 Vue 實例方法,通過把它們添加到 Vue.prototype 上實現。
  • 5.一個庫,提供自己的 API,同時提供上面提到的一個或多個功能,如 vue-router
MyPlugin.install = function (Vue, options) {
  // 1. 添加全局方法或屬性
  Vue.myGlobalMethod = function () {
    // 邏輯...
  }

  // 2. 添加全局資源
  Vue.directive('my-directive', {
    bind (el, binding, vnode, oldVnode) {
      // 邏輯...
    }
    ...
  })
 Vue.filter('toUpperCase',function(value){
     return value.toUpperCase();
 });
 Vue.mixin({
  methods:{
    demo:function(){
      alert(1);
    }
  }
});
//全局混入 直接調取方法

  // 3. 注入組件
  Vue.component(countDown.name,countDown)
  Vue.mixin({
    created: function () {
      // 邏輯...
    }
    ...
  })

  // 4. 添加實例方法
  Vue.prototype.$myMethod = function (methodOptions) {
    // 邏輯...
  }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM