1. 程式人生 > >vue自定義外掛

vue自定義外掛

官網教程

官網連結

開發外掛

外掛通常會為 Vue 新增全域性功能。外掛的範圍沒有限制——一般有下面幾種:
新增全域性方法或者屬性,如: vue-custom-element
新增全域性資源:指令/過濾器/過渡等,如 vue-touch
通過全域性 mixin 方法新增一些元件選項,如: vue-router
新增 Vue 例項方法,通過把它們新增到 Vue.prototype 上實現。
一個庫,提供自己的 API,同時提供上面提到的一個或多個功能,如 vue-router
Vue.js 的外掛應當有一個公開方法 install 。這個方法的第一個引數是 Vue 構造器,第二個引數是一個可選的選項物件:

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) {
    // 邏輯...
  }
}

使用外掛

通過全域性方法 Vue.use() 使用外掛:
// 呼叫 MyPlugin.install(Vue)
Vue.use(MyPlugin)
也可以傳入一個選項物件:
Vue.use(MyPlugin, { someOption: true })
Vue.use 會自動阻止多次註冊相同外掛,屆時只會註冊一次該外掛。
Vue.js 官方提供的一些外掛 (例如 vue-router) 在檢測到 Vue 是可訪問的全域性變數時會自動呼叫 Vue.use()。然而在例如 CommonJS 的模組環境中,你應該始終顯式地呼叫 Vue.use():
// 用 Browserify 或 webpack 提供的 CommonJS 模組環境時
var Vue = require(‘vue’)
var VueRouter = require(‘vue-router’)

// 不要忘了呼叫此方法
Vue.use(VueRouter)
awesome-vue 集合了來自社群貢獻的數以千計的外掛和庫。

根據官網教程自己寫個例子

新建一個.vue 的頁面aa.vue

<template>
    <div>
      <h1>loadding</h1>
    </div>
</template>

<script>
export default {
  name: 'aa'
}
</script>

<style scoped>

</style>

然後把這 aa.vue 做成外掛,如何做呢?再建一個.js,下面是根據官網改寫的,新加了一句Vue.component('Loading', loading)

import loading from './aa'
const MyPlugin = {
  install: function (Vue, options) {
    Vue.component('Loading', loading)
    // 1. 新增全域性方法或屬性
    Vue.myGlobalMethod = function () {
      console.log('ddddddd')
      // 邏輯...
    }

    // 2. 新增全域性資源
    Vue.directive('my-directive', {
      bind (el, binding, vnode, oldVnode) {
       el.style.backgroundColor = binding.value
        // 邏輯...
      }
    })

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

    // 4. 新增例項方法
    Vue.prototype.$myMethod = function (methodOptions) {
      console.log('ccccccccc')
      // 邏輯...
    }
  }
}

export default MyPlugin

外掛做好了,就這麼簡單。下面說說上面的幾個方法
Vue.component('Loading', loading)定義一個自定義元件,在 main.js應用後,可以在任何地方應用這個元件
Vue.myGlobalMethod定義 Vue 一個全域性的方法,後面我會出程式碼說怎麼用。

Vue.directive('my-directive',定義自定義的指令,後面有個簡單例子,可以看官網
Vue.mixin({混入,這個不多說,應用中應用的不多。官網地址
Vue.prototype.$myMethod自定義的例項方法,可以在.vue 中利用 this.myMethod 呼叫。

應用元件

修改 main.js,首先先引入自定義的外掛import my from '../test/vueinstall',然後利用 Vue.use 應用外掛Vue.use(my)

修改後如下

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import my from '../test/vueinstall'

Vue.config.productionTip = false
Vue.use(my)
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

新建.vue檔案,在 vue檔案中應用外掛。

<template>
  <div class="hello">
    <Loading v-my-directive="messages"></Loading>// 應用元件 Loading
    <div v-my-directive="messages">aaaaa</div>//應用自定義外掛
  </div>
</template>

<script>
import Vue from 'vue'
export default {
  name: 'HelloWorld',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App',
      messages: 'red'
    }
  },
  mounted () {
    Vue.myGlobalMethod()//vue 全域性方法使用
    this.$myMethod()//在例項中使用
  },
  methods: {
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>

這就是自定義的外掛使用,同樣在使用其他的外掛也是這樣,比如 element-ui,

import Element from 'element-ui'
Vue.use(Element)

這樣就可以全域性使用 element 的元件了。