vue實(shí)現(xiàn)自定義"模態(tài)彈窗"組件實(shí)例代碼
前言
對(duì)話框是很常用的組件 , 在很多地方都會(huì)用到,一般我們可以使用自帶的alert來(lái)彈出對(duì)話框,但是假如是設(shè)計(jì)出的圖該怎么辦呢 ,所以我們需要自己寫(xiě)一個(gè)對(duì)話框,下面來(lái)一起看看詳細(xì)的實(shí)現(xiàn)過(guò)程。
效果圖


以上截圖,紅色邊框部分,表示 “文字、圖標(biāo)或者圖片” 是可更改部分
實(shí)例代碼
一、創(chuàng)建彈窗組件 quitDialog.vue 組件
<template>
<transition-group name='fade'>
<!-- 退出彈窗 -->
<div class="quit_dialog"
key="1"
@click="isQuit = false"
v-if="isQuit"
@touchmove.prevent>
</div>
<div class="quit_box"
v-show="isQuit"
key="2">
<img :src="imgUrl"
:alt="imgLoadTip">
<div class="quit_title">{{title}}</div>
<p>{{content}}</p>
<button class="quit_btn" @click="leftClick">{{btnText}}</button>
<button class="quit_close" @click="rightClick">{{rightText}}</button>
</div>
</transition-group>
</template>
<script>
export default {
name: 'Popup',
data () {
return {
isQuit: false,
imgUrl: '',
title: '',
content: '',
btnText: '',
rightText: ''
}
},
methods: {
leftClick () {
this.leftBtn()
this.isQuit = false
},
rightClick () {
this.rightBtn()
this.isQuit = false
}
}
}
</script>
<style lang="scss" scoped>
// 退出彈窗
.fade-enter,
.fade-leave-active {
opacity: 0;
}
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.35s;
}
// 全局彈窗
.quit_dialog {
background: rgba(0,0,0,.5);
position: fixed;
top: 0;
left: 0;
height: 100%;
width: 100%;
z-index: 10000;
}
.quit_box {
width: 700px;
background: #fff;
position: fixed;
top: 50%;
left: 50%;
margin-left: -350px;
margin-top: -190px;
z-index: 10001;
border-radius: 10px;
text-align: center;
padding: 50px;
img{
width: 80px;
}
.quit_title{
color: #666;
font-size: 28px;
margin: 45px 0px;
}
button {
border-radius: 32px;
padding:20px 0px;
font-size: 26px;
border-radius: 8px;
width: 214px;
}
.quit_btn{
color: #03BA82;
background: #fff;
border: 1px solid #03BA82;
margin-right: 32px;
}
.quit_close {
background: linear-gradient(0deg, #03BA82, #01D695);
box-shadow: 0px 3px 4px 0px rgba(1, 84, 58, 0.27);
border: 1px solid #03BA82;
color: #fff;
}
}
</style>
二、創(chuàng)建 graspDialog.js
import Vue from 'vue'
import Grasp from '../components/QuitDialog/QuitDialog'
const PopupBox = Vue.extend(Grasp)
Grasp.install = function (data) {
let instance = new PopupBox({
data
}).$mount()
document.body.appendChild(instance.$el)
Vue.nextTick(() => {
instance.isQuit = true
// isQuit 和彈窗組件里的isQuit對(duì)應(yīng),用于控制顯隱
})
}
export default Grasp
三、在全局 main.js 引入
import Vue from 'vue' import Popup from './api/quitDialog' Vue.prototype.$popup = Popup.install
四、頁(yè)面中調(diào)用,只需在函數(shù)中調(diào)用即可
methods: {
graspBtn () {
this.$grasp({
imgUrl: require('../../assets/home/quits.png'), // 頂部圖片.
imgLoadTip: '圖片加載中...',
content: '溫馨提示',
title: '注意:該學(xué)習(xí)任務(wù)未完成,是否確認(rèn)退出',
btnText: '殘忍退出',
rightText: '繼續(xù)學(xué)習(xí)',
// 左邊點(diǎn)擊事件
leftBtn: () => {
this.$store.dispatch('user/logout').then(() => {
this.$signalr.LogoutPad()
this.$signalr.SendMsg(2, 0, '退出系統(tǒng)')
this.$router.push('/login')
})
},
// 右邊點(diǎn)擊事件
rightBtn: () => {}
})
}
}
總結(jié)
到此這篇關(guān)于vue實(shí)現(xiàn)自定義"模態(tài)彈窗"組件的文章就介紹到這了,更多相關(guān)vue自定義"模態(tài)彈窗"組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue Router4與Router3路由配置與區(qū)別說(shuō)明
這篇文章主要介紹了Vue Router4與Router3路由配置與區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-12-12
vue3.0基于views批量實(shí)現(xiàn)動(dòng)態(tài)路由的方法(示例代碼)
以前vue項(xiàng)目中也有很多實(shí)現(xiàn)動(dòng)態(tài)路由的方法,比如有一些項(xiàng)目涉及權(quán)限的可能會(huì)使用api請(qǐng)求路由數(shù)據(jù)在來(lái)createRouter,或者本地構(gòu)建使用routes.push來(lái)動(dòng)態(tài)構(gòu)建路由, 今天介紹一種新的方式來(lái)基于某個(gè)文件夾批量構(gòu)建動(dòng)態(tài)路由的方法,感興趣的朋友一起看看吧2024-12-12
Vue3使用ECharts實(shí)現(xiàn)?;鶊D的代碼示例
?;鶊D是一種用于直觀顯示流向數(shù)據(jù)的可視化工具,特別適合展示復(fù)雜的網(wǎng)絡(luò)關(guān)系和資源流動(dòng),在前端項(xiàng)目中,通過(guò)結(jié)合?Vue?3?和?ECharts,可以快速實(shí)現(xiàn)交互性強(qiáng)、樣式美觀的?;鶊D,本文將通過(guò)完整的代碼示例,帶你一步步完成一個(gè)?;鶊D的實(shí)現(xiàn),需要的朋友可以參考下2025-01-01
vue-router中hash模式與history模式的區(qū)別
為了構(gòu)建 SPA(單頁(yè)面應(yīng)用),需要引入前端路由系統(tǒng),這就是 Vue-Router 存在的意義,而這篇文章主要給大家介紹了關(guān)于vue-router中兩種模式區(qū)別的相關(guān)資料,分別是hash模式、history模式,需要的朋友可以參考下2021-06-06
Vue-Cli項(xiàng)目?jī)?yōu)化操作的實(shí)現(xiàn)
這篇文章主要介紹了Vue-Cli項(xiàng)目?jī)?yōu)化操作,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
vue?cli3?項(xiàng)目中如何使用axios發(fā)送post請(qǐng)求
這篇文章主要介紹了vue?cli3?項(xiàng)目中如何使用axios發(fā)送post請(qǐng)求,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue3中g(shù)etCurrentInstance獲取組件實(shí)例踩坑詳細(xì)記錄
getCurrentInstance()是Vue.js3?Composition?API中的一個(gè)函數(shù),它的作用是獲取當(dāng)前組件的實(shí)例對(duì)象,下面這篇文章主要給大家介紹了關(guān)于vue3中g(shù)etCurrentInstance獲取組件踩坑的相關(guān)資料,需要的朋友可以參考下2024-02-02

