【vue開發】vue插件的install方法


MyPlugin.install = function (Vue, options) {
  // 1. 添加全局方法或屬性
  Vue.myGlobalMethod = function () {
    // 邏輯...
  }

  // 2. 添加全局資源
  Vue.directive('my-directive', {
    bind (el, binding, vnode, oldVnode) {
      // 邏輯...
    }
    ...
  })

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

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

import VueI18n from 'vue-i18n' Vue.use(VueI18n)

這里注意的就是vue插件的使用方法,通過全局方法 Vue.use() 使用插件。

插件通常會為 Vue 添加全局功能。插件的范圍沒有限制——一般有下面幾種:添加全局方法或者屬性;添加全局資源:指令/過濾器/過渡等;通過全局 mixin 方法添加一些組件選項;添加 Vue 實例方法,通過把它們添加到 Vue.prototype 上實現。

了解vue插件的install方法對我們寫大型項目有很大幫助。

 


免責聲明!

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



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