日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Vue.js插件開發(fā)詳解

 

隨著 Vue.js 越來(lái)越火,Vue.js 的相關(guān)插件也在不斷的被貢獻(xiàn)出來(lái),數(shù)不勝數(shù)。比如官方推薦的 vue-router、vuex 等,都是非常優(yōu)秀的插件。但是我們更多的人還只停留在使用的階段,比較少自己開發(fā)。所以接下來(lái)會(huì)通過(guò)一個(gè)簡(jiǎn)單的 vue-toast 插件,來(lái)了解掌握插件的開發(fā)和使用。

認(rèn)識(shí)插件

想要開發(fā)插件,先要認(rèn)識(shí)一個(gè)插件是什么樣子的。

Vue.js 的插件應(yīng)當(dāng)有一個(gè)公開方法 install 。這個(gè)方法的***個(gè)參數(shù)是 Vue 構(gòu)造器 , 第二個(gè)參數(shù)是一個(gè)可選的選項(xiàng)對(duì)象:

 
 
 
 
  1. MyPlugin.install = function (Vue, options) { 
  2.   Vue.myGlobalMethod = function () {  // 1. 添加全局方法或?qū)傩?,? vue-custom-element 
  3.     // 邏輯... 
  4.   } 
  5.   Vue.directive('my-directive', {  // 2. 添加全局資源:指令/過(guò)濾器/過(guò)渡等,如 vue-touch 
  6.     bind (el, binding, vnode, oldVnode) { 
  7.       // 邏輯... 
  8.     } 
  9.     ... 
  10.   }) 
  11.   Vue.mixin({ 
  12.     created: function () {  // 3. 通過(guò)全局 mixin方法添加一些組件選項(xiàng),如: vuex 
  13.       // 邏輯... 
  14.     } 
  15.     ... 
  16.   }) 
  17.   Vue.prototype.$myMethod = function (options) {  // 4. 添加實(shí)例方法,通過(guò)把它們添加到 Vue.prototype 上實(shí)現(xiàn) 
  18.     // 邏輯... 
  19.   } 

接下來(lái)要講到的 vue-toast 插件則是通過(guò)添加實(shí)例方法實(shí)現(xiàn)的。我們先來(lái)看個(gè)小例子。先新建個(gè)js文件來(lái)編寫插件:toast.js

 
 
 
 
  1. // toast.js 
  2. var Toast = {}; 
  3. Toast.install = function (Vue, options) { 
  4.     Vue.prototype.$msg = 'Hello World'; 
  5. module.exports = Toast; 

在 main.js 中,需要導(dǎo)入 toast.js 并且通過(guò)全局方法 Vue.use() 來(lái)使用插件:

 
 
 
 
  1. // main.js 
  2. import Vue from 'vue'; 
  3. import Toast from './toast.js'; 
  4. Vue.use(Toast); 

然后,我們?cè)诮M件中來(lái)獲取該插件定義的 $msg 屬性。

 
 
 
 
  1. // App.vue 
  2. export default { 
  3.     mounted(){ 
  4.         console.log(this.$msg);         // Hello World 
  5.     } 

可以看到,控制臺(tái)成功的打印出了 Hello World 。既然 $msg 能獲取到,那么我們就可以來(lái)實(shí)現(xiàn)我們的 vue-toast 插件了。

開發(fā) vue-toast

需求:在組件中通過(guò)調(diào)用 this.$toast(‘網(wǎng)絡(luò)請(qǐng)求失敗’) 來(lái)彈出提示,默認(rèn)在底部顯示??梢酝ㄟ^(guò)調(diào)用 this.$toast.top() 或 this.$toast.center() 等方法來(lái)實(shí)現(xiàn)在不同位置顯示。

整理一下思路,彈出提示的時(shí)候,我可以在 body 中添加一個(gè) div 用來(lái)顯示提示信息,不同的位置我通過(guò)添加不同的類名來(lái)定位,那就可以開始寫了。

 
 
 
 
  1. // toast.js 
  2. var Toast = {}; 
  3. Toast.install = function (Vue, options) { 
  4.     Vue.prototype.$toast = (tips) => { 
  5.         let toastTpl = Vue.extend({     // 1、創(chuàng)建構(gòu)造器,定義好提示信息的模板 
  6.             template: '' + tips + '
  •         }); 
  •         let tpl = new toastTpl().$mount().$el;  // 2、創(chuàng)建實(shí)例,掛載到文檔以后的地方 
  •         document.body.appendChild(tpl);     // 3、把創(chuàng)建的實(shí)例添加到body中 
  •         setTimeout(function () {        // 4、延遲2.5秒后移除該提示 
  •             document.body.removeChild(tpl); 
  •         }, 2500) 
  •     } 
  • module.exports = Toast; 
  • 好像很簡(jiǎn)單,我們就實(shí)現(xiàn)了 this.$toast() ,接下來(lái)顯示不同位置。

     
     
     
     
    1. // toast.js 
    2. ['bottom', 'center', 'top'].forEach(type => { 
    3.     Vue.prototype.$toast[type] = (tips) => { 
    4.         return Vue.prototype.$toast(tips,type) 
    5.     } 
    6. }) 

    這里把 type 傳給 $toast 在該方法里進(jìn)行不同位置的處理,上面說(shuō)了通過(guò)添加不同的類名(toast-bottom、toast-top、toast-center)來(lái)實(shí)現(xiàn),那 $toast 方法需要小小修改一下。

     
     
     
     
    1. Vue.prototype.$toast = (tips,type) => {     // 添加 type 參數(shù) 
    2.     let toastTpl = Vue.extend({             // 模板添加位置類 
    3.         template: '' + tips + '
  •     }); 
  •     ... 
  • 好像差不多了。但是如果我想默認(rèn)在頂部顯示,我每次都要調(diào)用 this.$toast.top() 好像就有點(diǎn)多余了,我能不能 this.$toast() 就直接在我想要的地方呢?還有我不想要 2.5s 后才消失呢?這時(shí)候注意到 Toast.install(Vue,options) 里的 options 參數(shù),我們可以在 Vue.use() 通過(guò) options 傳進(jìn)我們想要的參數(shù)。***修改插件如下:

     
     
     
     
    1. var Toast = {}; 
    2. Toast.install = function (Vue, options) { 
    3.     let opt = { 
    4.         defaultType:'bottom',   // 默認(rèn)顯示位置 
    5.         duration:'2500'         // 持續(xù)時(shí)間 
    6.     } 
    7.     for(let property in options){ 
    8.         opt[property] = options[property];  // 使用 options 的配置 
    9.     } 
    10.     Vue.prototype.$toast = (tips,type) => { 
    11.         if(type){ 
    12.             opt.defaultType = type;         // 如果有傳type,位置則設(shè)為該type 
    13.         } 
    14.         if(document.getElementsByClassName('vue-toast').length){ 
    15.             // 如果toast還在,則不再執(zhí)行 
    16.             return; 
    17.         } 
    18.         let toastTpl = Vue.extend({ 
    19.             template: '' + tips + '
  •         }); 
  •         let tpl = new toastTpl().$mount().$el; 
  •         document.body.appendChild(tpl); 
  •         setTimeout(function () { 
  •             document.body.removeChild(tpl); 
  •         }, opt.duration) 
  •     } 
  •     ['bottom', 'center', 'top'].forEach(type => { 
  •         Vue.prototype.$toast[type] = (tips) => { 
  •             return Vue.prototype.$toast(tips,type) 
  •         } 
  •     }) 
  • module.exports = Toast; 
  • 這樣子一個(gè)簡(jiǎn)單的 vue 插件就實(shí)現(xiàn)了,并且可以通過(guò) npm 打包發(fā)布,下次就可以使用 npm install 來(lái)安裝了。

    源碼地址:vue-toast

    【本文為專欄作者“林鑫”的原創(chuàng)稿件,轉(zhuǎn)載請(qǐng)通過(guò)微信公眾號(hào)聯(lián)系作者獲取授權(quán)】


    新聞標(biāo)題:Vue.js插件開發(fā)詳解
    URL地址:http://www.dlmjj.cn/article/cocishi.html