1. 簡介
本節(jié)我們將介紹 Vue 的插件诵盼。包括什么是插件惠豺、如何使用插件、如何編寫一個簡單的插件风宁。其中洁墙,編寫和使用插件是本節(jié)的重點。本節(jié)我們將帶領(lǐng)大家寫一個簡單的插件示例杀糯,同學(xué)們在學(xué)完本節(jié)后可以嘗試編寫其他的插件來加深學(xué)習(xí)扫俺。
2. 木子解釋
插件通常用來為 Vue 添加全局功能苍苞。插件的功能范圍沒有嚴(yán)格的限制固翰,一般有下面幾種:
- 添加全局方法或者屬性。如: vue-custom-element羹呵。
- 添加全局資源:指令 / 過濾器 / 過渡等骂际。如 vue-touch。
- 通過全局混入來添加一些組件選項冈欢。如 vue-router歉铝。
- 添加 Vue 實例方法,通過把它們添加到
Vue.prototype
上實現(xiàn)凑耻。- 一個庫太示,提供自己的 API,同時提供上面提到的一個或多個功能香浩。如 vue-router类缤。
Vue 插件是對 Vue 全局功能的擴展,他可以給 Vue 添加全局方法邻吭、屬性餐弱、組件、過濾器、指令等等膏蚓。
3. 使用插件
通過全局方法 Vue.use () 使用插件瓢谢。它需要在你調(diào)用 new Vue () 啟動應(yīng)用之前完成:
Vue.use(MyPlugin)
new Vue({
// ...組件選項
})
代碼塊12345
也可以傳入一個可選的選項對象:
Vue.use(MyPlugin, { someOption: true })
代碼塊1
Vue.use 會自動阻止多次注冊相同插件,即使多次調(diào)用也只會注冊一次該插件驮瞧。
Vue.js 官方提供的一些插件 (例如 vue-router) 在檢測到 Vue 是可訪問的全局變量時會自動調(diào)用 Vue.use ()氓扛。然而在像 CommonJS 這樣的模塊環(huán)境中,你應(yīng)該始終顯式地調(diào)用 Vue.use ():
// 用 Browserify 或 webpack 提供的 CommonJS 模塊環(huán)境時
var Vue = require('vue')
var VueRouter = require('vue-router')
// 不要忘了調(diào)用此方法
Vue.use(VueRouter)
代碼塊123456
awesome-vue 集合了大量由社區(qū)貢獻(xiàn)的插件和庫论笔。
4. 開發(fā)插件
Vue.js 的插件應(yīng)該暴露一個 install 方法幢尚。這個方法的第一個參數(shù)是 Vue 構(gòu)造器,第二個參數(shù)是一個可選的選項對象:
const MyPlugin = {}
MyPlugin.install = function (Vue, options) {
// 1. 添加全局方法或?qū)傩? Vue.myGlobalMethod = function () {
// 邏輯...
}
// 2. 添加全局資源
Vue.directive('my-directive', {
bind (el, binding, vnode, oldVnode) {
// 邏輯...
}
...
})
// 3. 注入組件選項
Vue.mixin({
created: function () {
// 邏輯...
}
...
})
// 4. 添加實例方法
Vue.prototype.$myMethod = function (methodOptions) {
// 邏輯...
}
}
代碼塊1234567891011121314151617181920212223242526272829
接下來翅楼,我們寫一個具體的插件示例:
實例演示
<!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. 小結(jié)
本節(jié)胎挎,我們帶大家學(xué)習(xí)了 Vue 插件的使用方式沟启。主要知識點有以下幾點:
- 通過 Vue.use (“插件名字”) 使用插件。
- 通過 install 方法開發(fā)插件犹菇。