成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

如何用vue實現模態框組件

開發 前端
基本上每個項目都需要用到模態框組件,由于在最近的項目中,alert組件和confirm是兩套完全不一樣的設計,所以我將他們分成了兩個組件,本文主要討論的是confirm組件的實現。

基本上每個項目都需要用到模態框組件,由于在最近的項目中,alert組件和confirm是兩套完全不一樣的設計,所以我將他們分成了兩個組件,本文主要討論的是confirm組件的實現。

組件結構 

  1. <template> 
  2.     <div class="modal" v-show="show" transition="fade"
  3.         <div class="modal-dialog"
  4.             <div class="modal-content"
  5.                 <!--頭部--> 
  6.                 <div class="modal-header"
  7.                     <slot name="header"
  8.                         <p class="title">{{modal.title}}</p> 
  9.                     </slot> 
  10.                     <a v-touch:tap="close(0)" class="close" href="javascript:void(0)"></a> 
  11.                 </div> 
  12.                 <!--內容區域--> 
  13.                 <div class="modal-body"
  14.                     <slot name="body"
  15.                         <p class="notice">{{modal.text}}</p> 
  16.                     </slot> 
  17.                 </div> 
  18.                 <!--尾部,操作按鈕--> 
  19.                 <div class="modal-footer"
  20.                     <slot name="button"
  21.                         <a v-if="modal.showCancelButton" href="javascript:void(0)" class="button {{modal.cancelButtonClass}}" v-touch:tap="close(1)">{{modal.cancelButtonText}}</a> 
  22.                         <a v-if="modal.showConfirmButton" href="javascript:void(0)" class="button {{modal.confirmButtonClass}}" v-touch:tap="submit">{{modal.confirmButtonText}}</a> 
  23.                     </slot> 
  24.                 </div> 
  25.             </div> 
  26.         </div> 
  27.     </div> 
  28.     <div v-show="show" class="modal-backup" transition="fade"></div> 
  29. </template>   

 模態框結構分為三部分,分別為頭部、內部區域和操作區域,都提供了slot,可以根據需要定制。

樣式 

  1. .modal { 
  2.     position: fixed; 
  3.     left: 0; 
  4.     top: 0; 
  5.     right: 0; 
  6.     bottom: 0; 
  7.     z-index: 1001; 
  8.     -webkit-overflow-scrolling: touch; 
  9.     outline: 0; 
  10.     overflow: scroll
  11.     margin: 30/@rate auto; 
  12. .modal-dialog { 
  13.     position: absolute
  14.     left: 50%; 
  15.     top: 0; 
  16.     transform: translate(-50%,0); 
  17.     width: 690/@rate; 
  18.     padding: 50/@rate 40/@rate; 
  19.     background: #fff; 
  20. .modal-backup { 
  21.     position: fixed; 
  22.     top: 0; 
  23.     right: 0; 
  24.     bottom: 0; 
  25.     left: 0; 
  26.     z-index: 1000; 
  27.     background: rgba(0, 0, 0, 0.5); 
  28.  

這里只是一些基本樣式,沒什么好說的,這次項目是在移動端,用了淘寶的自適應布局方案,@rate是切稿時候的轉換率。

接口定義 

  1. /** 
  2.  * modal 模態接口參數 
  3.  * @param {string} modal.title 模態框標題 
  4.  * @param {string} modal.text 模態框內容 
  5.  * @param {boolean} modal.showCancelButton 是否顯示取消按鈕 
  6.  * @param {string} modal.cancelButtonClass 取消按鈕樣式 
  7.  * @param {string} modal.cancelButtonText 取消按鈕文字 
  8.  * @param {string} modal.showConfirmButton 是否顯示確定按鈕 
  9.  * @param {string} modal.confirmButtonClass 確定按鈕樣式 
  10.  * @param {string} modal.confirmButtonText 確定按鈕標文字 
  11.  */ 
  12. props: ['modalOptions'], 
  13. computed: { 
  14.     /** 
  15.      * 格式化props進來的參數,對參數賦予默認值 
  16.      */ 
  17.     modal: { 
  18.         get() { 
  19.             let modal = this.modalOptions; 
  20.             modal = { 
  21.                 title: modal.title || '提示'
  22.                 text: modal.text, 
  23.                 showCancelButton: typeof modal.showCancelButton === 'undefined' ? true : modal.showCancelButton, 
  24.                 cancelButtonClass: modal.cancelButtonClass ? modal.showCancelButton : 'btn-default'
  25.                 cancelButtonText: modal.cancelButtonText ? modal.cancelButtonText : '取消'
  26.                 showConfirmButton: typeof modal.showConfirmButton === 'undefined' ? true : modal.cancelButtonClass, 
  27.                 confirmButtonClass: modal.confirmButtonClass ? modal.confirmButtonClass : 'btn-active'
  28.                 confirmButtonText: modal.confirmButtonText ? modal.confirmButtonText : '確定'
  29.             }; 
  30.             return modal; 
  31.         }, 
  32.     }, 
  33. },  

這里定義了接口的參數,可以自定義標題、內容、是否顯示按鈕和按鈕的樣式,用一個computed來做參數默認值的控制。

模態框內部方法 

  1. data() { 
  2.     return { 
  3.         show: false,   // 是否顯示模態框 
  4.         resolve: ''
  5.         reject: ''
  6.         promise: '',  // 保存promise對象 
  7.     }; 
  8. }, 
  9. methods: { 
  10.     /** 
  11.      * 確定,將promise斷定為完成態 
  12.      */ 
  13.     submit() { 
  14.         this.resolve('submit'); 
  15.     }, 
  16.     /** 
  17.      * 關閉,將promise斷定為reject狀態 
  18.      * @param type {number} 關閉的方式 0表示關閉按鈕關閉,1表示取消按鈕關閉 
  19.      */ 
  20.     close(type) { 
  21.         this.show = false
  22.         this.reject(type); 
  23.     }, 
  24.     /** 
  25.      * 顯示confirm彈出,并創建promise對象 
  26.      * @returns {Promise} 
  27.      */ 
  28.     confirm() { 
  29.         this.show = true
  30.         this.promise = new Promise((resolve, reject) => { 
  31.             this.resolve = resolve; 
  32.             this.reject = reject; 
  33.         }); 
  34.         return this.promise;   //返回promise對象,給父級組件調用 
  35.     }, 
  36. },  

在模態框內部定義了三個方法,最核心部分confirm方法,這是一個定義在模態框內部,但是是給使用模態框的父級組件調用的方法,該方法返回的是一個promise對象,并將resolve和reject存放于modal組件的data中,點擊取消按鈕時,斷定為reject狀態,并將模態框關閉掉,點確定按鈕時,斷定為resolve狀態,模態框沒有關閉,由調用modal組件的父級組件的回調處理完成后手動控制關閉模態框。

調用 

  1. <!-- template --> 
  2. <confirm v-ref:dialog :modal-options.sync="modal"></confirm> 
  3. <!-- methods --> 
  4. this.$refs.dialog.confirm().then(() => { 
  5.     // 點擊確定按鈕的回調處理 
  6.     callback(); 
  7.     this.$refs.dialog.show = false;  
  8. }).catch(() => { 
  9.     // 點擊取消按鈕的回調處理 
  10.     callback(); 
  11. });  

用v-ref創建一個索引,就很方便拿到模態框組件內部的方法了。這樣一個模態框組件就完成了。

其他實現方法

在模態框組件中,比較難實現的應該是點擊確定和取消按鈕時,父級的回調處理,我在做這個組件時,也參考了一些其實實現方案。

使用事件轉發

這個方法是我的同事實現的,用在上一個項目,采用的是$dispatch和$broadcast來派發或廣播事件。

首先在根組件接收dispatch過來的transmit事件,再將transmit事件傳遞過來的eventName廣播下去 

  1. events: { 
  2.     /** 
  3.      * 轉發事件 
  4.      * @param  {string} eventName 事件名稱 
  5.      * @param  {object} arg       事件參數 
  6.      * @return {null
  7.      */ 
  8.     'transmit'function (eventName, arg) { 
  9.         this.$broadcast(eventName, arg); 
  10.     } 
  11. },  

其次是模態框組件內部接收從父級組件傳遞過來的確定和取消按鈕所觸發的事件名,點擊取消和確定按鈕的時候觸發 

  1. // 接收事件,獲得需要取消和確定按鈕的事件名 
  2. events: { 
  3.     'tip'function(obj) { 
  4.         this.events = { 
  5.             cancel: obj.events.cancel, 
  6.             confirm: obj.events.confirm 
  7.         } 
  8.     } 
  9. // 取消按鈕 
  10. cancel:function() { 
  11.     this.$dispatch('transmit',this.events.cancel); 
  12. // 確定按鈕 
  13. submit: function() { 
  14.     this.$dispatch('transmit',this.events.submit); 
  15.  

在父級組件中調用模態框如下: 

  1. this.$dispatch('transmit','tip',{ 
  2.     events: { 
  3.         confirm: 'confirmEvent' 
  4.     } 
  5. }); 
  6. this.$once('confirmEvent',function() { 
  7.     callback(); 
  8.  

先是傳遞tip事件,將事件名傳遞給模態框,再用$once監聽確定或取消按鈕所觸發的事件,事件觸發后進行回調。

這種方法看起來是不是很暈?所以vue 2.0取消了$dispatch和$broadcast,我們在最近的項目中雖然還在用1.0,但是也不再用$dispatch和$broadcast,方便以后的升級。

使用emit來觸發

這種方法來自vue-bootstrap-modal,點擊取消和確定按鈕的時候分別emit一個事件,直接在組件上監聽這個事件,這種做法的好處是事件比較容易追蹤。 

  1. // 確定按鈕 
  2. ok () { 
  3.     this.$emit('ok'); 
  4.     if (this.closeWhenOK) { 
  5.         this.show = false
  6.     } 
  7. }, 
  8. // 取消按鈕 
  9. cancel () { 
  10.     this.$emit('cancel'); 
  11.     this.show = false
  12. },  

調用:

  1. <modal title="Modal Title" :show.sync="show" @ok="ok" @cancel="cancel"
  2.     Modal Text 
  3. </modal>  

但是我們在使用的時候經常會遇到這樣的場景,在一個組件的內部,經常會用到多個對話框,對話框可能只是文字有點區別,回調不同,這時就需要在template中為每個對話框都寫一次<modal></modal>,有點麻煩。不想每次寫,可以用v-for來遍歷,這篇文章關于 vue 彈窗組件的一些感想有我與作者的討論,可以參考一下。

參考資料

責任編輯:龐桂玉 來源: segmentfault
相關推薦

2017-10-11 16:19:36

jquery留言框設計

2023-05-17 10:05:35

組件設計(Modal)組件

2016-09-06 19:45:18

javascriptVue前端

2011-07-01 11:33:00

Qt 模態 非模態

2011-03-15 14:26:23

iptablesNAT

2022-02-20 19:02:16

RollupVue 2JavaScrip

2011-03-15 09:10:47

iptablesNAT

2024-01-23 09:15:33

Vue3組件拖拽組件內容編輯

2020-05-09 10:38:31

Python透視表數據

2018-04-16 14:39:10

Vue輪播切換

2024-08-13 09:26:07

2016-09-19 13:44:54

vue翻頁組件Web

2020-02-21 11:08:24

瀏覽器HTML設計

2010-05-24 10:23:34

實現MySQL

2015-07-22 12:42:36

Pivot行列轉換

2017-10-27 22:03:35

javascrip

2020-09-24 14:06:19

Vue

2023-03-29 08:52:58

視覺Vue組件庫

2016-09-19 21:37:58

vue特效組件Web

2022-04-26 05:55:06

Vue.js異步組件
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲精品一区二区二区 | 国产草草视频 | 国产日韩欧美一区二区 | 亚洲一区中文字幕 | 久久久噜噜噜久久中文字幕色伊伊 | 欧美黄色一级毛片 | 91视频免费观看 | 国产乱码精品一区二区三区av | 亚洲国产福利视频 | 激情网站| 中文字幕一区二区三区四区 | 欧美一区二区三区免费在线观看 | 日本久久网 | 久久久久久一区 | 色综合天天天天做夜夜夜夜做 | 国外激情av | 日韩视频免费 | 毛片视频网站 | a在线免费观看视频 | 国产精品久久久久久久久久妞妞 | 欧美一区二区三区视频 | 国产一区亚洲 | 久久伊人青青草 | 日日爱夜夜操 | 欧美一级特黄aaa大片在线观看 | 一区二区在线不卡 | 精品久久久久久亚洲综合网 | 一区二区三区视频在线观看 | 91精品国产91久久久久久最新 | 国产欧美日韩一区二区三区 | 亚洲精品国产综合区久久久久久久 | 亚洲成人一区二区 | 欧美一区二区三区视频在线播放 | 天堂在线www | 成人精品一区二区三区 | 欧美精品91爱爱 | 欧美成人a | 日本高清精品 | 欧美日韩三级在线观看 | 99综合在线 | 亚洲精品电影在线观看 |