使用vue實現(xiàn)各類彈出框組件
簡單介紹一下vue中常用dialog組件的封裝:
實現(xiàn)動態(tài)傳入內(nèi)容,實現(xiàn)取消,確認(rèn)等回調(diào)函數(shù)。
首先寫一個基本的彈窗樣式,如上圖所示。
在需要用到彈窗的地方中引入組件:
import dialogBar from './dialog.vue' components:{ 'dialog-bar': dialogBar, }, <dialog-bar></dialog-bar>
點擊一個按鈕顯示彈窗,并保證關(guān)閉彈窗后再次點擊依舊顯示
在彈窗組件中定義一個value值:v-model="sendVal",
sendVal初始值為false。
在打開彈窗的方法中賦值:
openMask(){ this.sendVal = true; }
在dialog.vue組件中做如下操作:
props: { value: {} // 注意此處獲取的value對應(yīng)的就是組件標(biāo)簽中的v-model }
定義一個showMask變量用于控制是否顯示彈窗
mounted(){ this.showMask = this.value; // 在生命周期中,把獲取的value值獲取給showMash }, watch:{ value(newVal, oldVal){ this.showMask = newVal; // 監(jiān)測value的變化,并賦值。 }, showMask(val) { this.$emit('input', val); // 此處監(jiān)測showMask目的為關(guān)閉彈窗時,重新更換value值,注意emit的事件一定要為input。 } },
而要想關(guān)閉彈窗,只需要定義一個方法:
closeMask(){ this.showMask = false; },
此刻已經(jīng)可以實現(xiàn)彈窗的顯示與退出。
下面我們要實現(xiàn)的是動態(tài)添加標(biāo)題,內(nèi)容等,在組件標(biāo)簽中加入title,content:
<dialog-bar title="我是標(biāo)題" content="我是內(nèi)容"></dialog-bar>
可以運(yùn)用vue的數(shù)據(jù)雙向綁定,更換title,content。
在dialog.vue中獲取內(nèi)容:
props: { value: {}, content: { type: String, default: '' }, title: { type: String, default: '' }, }, <div class="dialog-title">{{title}}</div> <div class="content" v-html="content"></div>
我們可以運(yùn)用同樣的原理來獲取不同按鈕中的自定名稱。
下面我們利用傳入的不同type來確定不同的按鈕,并提供不同的回調(diào)函數(shù)。
<dialog-bar title="我是標(biāo)題" content="我是內(nèi)容" type="danger" dangerText="這是刪除按鈕"></dialog-bar>
如傳入type為danger,我們可以在dialog.vue中props獲取type,并定義一個如下按鈕:
<div v-if="type == 'danger'" class="danger-btn" @click="dangerBtn"> {{dangerText}} </div> dangerBtn(){ this.$emit('danger'); // 發(fā)送一個danger事件作為回調(diào)函數(shù) this.closeMask(); // 關(guān)閉彈窗 },
在標(biāo)簽中定義danger的回調(diào)并做一些操作:
<dialog-bar title="我是標(biāo)題" content="我是內(nèi)容" type="danger" dangerText="這是刪除按鈕" @danger="clickDanger()"></dialog-bar> clickDanger(){ console.log("這里是回調(diào)函數(shù)") },
同樣原理可以獲取和增添一些其他的操作:
props: { value: {}, // 類型包括 defalut 默認(rèn), danger 危險, confirm 確認(rèn), type:{ type: String, default: 'default' }, content: { type: String, default: '' }, title: { type: String, default: '' }, confirmText: { type: String, default: '確認(rèn)' }, cancelText: { type: String, default: '取消' }, dangerText: { type: String, default: '刪除' }, }, <div class="btns"> <div v-if="type != 'confirm'" class="default-btn" @click="closeBtn"> {{cancelText}} </div> <div v-if="type == 'danger'" class="danger-btn" @click="dangerBtn"> {{dangerText}} </div> <div v-if="type == 'confirm'" class="confirm-btn" @click="confirmBtn"> {{confirmText}} </div> </div>
點擊此處去github下載彈窗代碼: https://github.com/wwjhzc/vue-dialog
總結(jié)
以上所述是小編給大家介紹的使用vue實現(xiàn)各類彈出框組件,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
關(guān)于antd中select搜索框改變搜索值的問題
這篇文章主要介紹了關(guān)于antd中select搜索框改變搜索值的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04vue中利用pinyin-pro純前端實現(xiàn)拼音的模糊搜索功能
這篇文章主要介紹了vue中利用pinyin-pro純前端實現(xiàn)拼音的模糊搜索,實現(xiàn)思路很簡單,通過安裝配置插件編寫工具類,本文結(jié)合實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11python虛擬環(huán)境 virtualenv的簡單使用
virtualenv是一個創(chuàng)建隔絕的Python環(huán)境的工具。這篇文章主要介紹了python虛擬環(huán)境 virtualenv的簡單使用,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01vue學(xué)習(xí)筆記之v-if和v-show的區(qū)別
本篇文章主要介紹了vue學(xué)習(xí)筆記之v-if和v-show的區(qū)別,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09vue實現(xiàn)搜索并高亮文字的兩種方式總結(jié)
在做文字處理的項目時經(jīng)常會遇到搜索文字并高亮的需求,常見的實現(xiàn)方式有插入標(biāo)簽和貼標(biāo)簽兩種,這兩種方式適用于不同的場景,各有優(yōu)劣,下面我們就來看看他們的具體實現(xiàn)吧2023-11-11