亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

1. 簡介

本節我們將介紹 Vue 的插件。包括什么是插件、如何使用插件、如何編寫一個簡單的插件。其中,編寫和使用插件是本節的重點。本節我們將帶領大家寫一個簡單的插件示例,同學們在學完本節后可以嘗試編寫其他的插件來加深學習。

2. 慕課解釋

插件通常用來為 Vue 添加全局功能。插件的功能范圍沒有嚴格的限制,一般有下面幾種:

  1. 添加全局方法或者屬性。如: vue-custom-element。
  2. 添加全局資源:指令 / 過濾器 / 過渡等。如 vue-touch。
  3. 通過全局混入來添加一些組件選項。如 vue-router。
  4. 添加 Vue 實例方法,通過把它們添加到 Vue.prototype 上實現。
  5. 一個庫,提供自己的 API,同時提供上面提到的一個或多個功能。如 vue-router。

Vue 插件是對 Vue 全局功能的擴展,他可以給 Vue 添加全局方法、屬性、組件、過濾器、指令等等。

3. 使用插件

通過全局方法 Vue.use () 使用插件。它需要在你調用 new Vue () 啟動應用之前完成:

Vue.use(MyPlugin)

new Vue({
  // ...組件選項
})

也可以傳入一個可選的選項對象:

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 集合了大量由社區貢獻的插件和庫。

4. 開發插件

Vue.js 的插件應該暴露一個 install 方法。這個方法的第一個參數是 Vue 構造器,第二個參數是一個可選的選項對象:

const MyPlugin = {}

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

接下來,我們寫一個具體的插件示例:

實例演示
預覽 復制
復制成功!
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <my-button>default</my-button>
    <div class="marigin"></div>
    <my-button type="default">default</my-button>
    <div class="marigin"></div>
    <my-button type="primary">primary</my-button>
    <div class="marigin"></div>
    <my-button type="warning">warning</my-button>
    <div class="marigin"></div>
    <my-button type="success">success</my-button>
  </div>
</body>
<style>
  .marigin{
    margin-top: 10px;
  }
  .button{
    width: 100%;
    height: 34px;
    line-height: 32px;
    outline: none;
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    cursor: pointer;
    
  }
  .button-default {
    background-color: #ffffff;
    color: #333333;
    border: 1px solid #ccc;
  }
  .button-primary {
    background-color: #39f;
    color: #ffffff;
    border: 1px solid #39f;
  }
  .button-warning {
    background-color: #f90;
    color: #ffffff;
    border: 1px solid #f90;
  }
  .button-success {
    background-color: #0c6;
    color: #ffffff;
    border: 1px solid #0c6;
  }
</style>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script type="text/javascript">
  // 定義 MyPlugin
  const MyPlugin = {}
  MyPlugin.install = function(Vue, options) {
    Vue.component('MyButton',{
      template: '<button :class="btnClass"><slot/></button>',
      props: {
        type: {
          type: String,
          default: 'default'
        }
      },
      computed: {
        btnClass() {
          return ["button",`button-${this.type}`]
        }
      }
    })
  }
  // 使用插件 MyPlugin
  Vue.use(MyPlugin)

  var vm = new Vue({
    el: '#app',
    data() {
    	return {}
    }
  })
</script>
</html>

運行案例 點擊 "運行案例" 可查看在線運行效果

代碼解釋:
JS 代碼第 3-20 行,我們定義了插件 MyPlugin,該插件中包含一個全局組件 MyButton。
JS 代碼第 22 行,通過 Vue.use 使用 MyPlugin。
HTML 代碼第 2、4、6、8、10 行,使用 MyPlugin 插件中的 MyButton 組件。

5. 小結

本節,我們帶大家學習了 Vue 插件的使用方式。主要知識點有以下幾點:

  • 通過 Vue.use (“插件名字”) 使用插件。
  • 通過 install 方法開發插件。