vue結(jié)合el-dialog封裝自己的confirm二次確認(rèn)彈窗方式
vue結(jié)合el-dialog封裝自己的confirm二次確認(rèn)彈窗
這里使用el-dialog 主要是用他的關(guān)閉動(dòng)畫(huà),讓關(guān)閉更加絲滑
首先在components 添加 ConfirmAlert文件夾 然后添加vue和js 文件

index.js
import Vue from 'vue';
import confirm from './index.vue'
let confirmConstructor = Vue.extend(confirm);
let theConfirm = function (content) {
return new Promise((res, rej) => {
//返回promise,ok執(zhí)行resolve,調(diào)用時(shí)使用.then繼續(xù),no執(zhí)行reject調(diào)用時(shí)使用catch捕捉
let confirmDom = new confirmConstructor({
el: document.createElement('div')
})
const elDiv = document.body.appendChild(confirmDom.$el); //new一個(gè)對(duì)象,然后插入body里面
confirmDom.content = content; //為了使confirm的擴(kuò)展性更強(qiáng),這個(gè)采用對(duì)象的方式傳入,所有的字段都可以根據(jù)需求自定義
confirmDom.ok = function () {
res()
close()
}
confirmDom.close = function () {
rej()
close()
}
function close() {
confirmDom.dialogVisible = false
setTimeout(() => {
console.log('remove');
elDiv.remove()
}, 1000);
}
})
}
export default theConfirm;index.vue
<template>
<div class="confirm-container">
<el-dialog :title="content.type" :before-close="close" :visible.sync="dialogVisible" width="30%">
<div class="confirm-content">
<p class="msgContent">{{ content.msg }}
</p>
<div class="foot" slot="footer">
<el-button type="primary" @click.stop="close">
<span>{{ content.btn.no || '確認(rèn)' }}</span>
</el-button>
<el-button type="primary" @click.stop="ok">
<span>{{ content.btn.ok || '取消' }}</span>
</el-button>
</div>
</div>
</el-dialog>
</div>
</template>
<script>
export default {
data() {
return {
content: {
type: '提示',
msg: '',
btn: {
ok: '確定',
no: '取消'
},
},
dialogVisible: true
}
},
methods: {
close() {
console.log('關(guān)閉');
},
ok() {
console.log('確定')
}
}
}
</script>
<style scoped>
.msgContent {
text-align: center;
}
.foot {
width: 100%;
display: flex;
justify-content: center;
margin-top: 32px;
}
</style>main.js將alertConfirm掛載vue上
import alertConfirm from '@/components/confirmAlert' Vue.prototype.$alertConfirm = alertConfirm;
組件中使用
<!-- -->
<template>
<div>
<el-button @click="btn">log</el-button>
</div>
</template>
<script>
export default {
data() {
return {
};
},
watch: {},
components: {},
computed: {},
created() { },
mounted() { },
methods: {
btn() {
let that = this // 存儲(chǔ)this指向
this.$alertConfirm({
type: '提示',
msg: '是否刪除這條信息?',
btn: {
ok: '確定', no: '取消'
},
//msgDetail: ''
}).then(() => {
// 確認(rèn)
// do something
that.logs('確認(rèn)')
}).catch(() => {
//取消
// do something
that.logs('取消')
})
},
logs(type) {
console.log('調(diào)用組件的' + type);
}
}
}
</script>
<!-- <style scoped>
</style> -->vue二次確認(rèn)彈窗組件
1.二次確認(rèn)彈窗組件reconfirm.vue
<template>
<el-dialog :visible="dialogFlag" @close="closeDialog()" width="420px" class="myz-info-reconfirm">
<div slot="title" class="title-reconfirm">
<span>{{title}}</span>
<span class="warn-red" v-if="deleteAdmin">({{redWarnMessage}})</span>
</div>
<div class="body_message">
<span class="warn-img"></span>
{{warnMessage}}
</div>
<span slot="footer" class="clearfix">
<span class="check-exact" v-if="deleteAdmin">
<el-checkbox v-model="checked">我已了解</el-checkbox>
</span>
<span class="check-btn">
<el-button type="primary" :disabled="!checked && deleteAdmin" @click="exact()">確定</el-button>
<el-button @click="closeDialog">取消</el-button>
</span>
</span>
</el-dialog>
</template>
<script>
import { mapState, mapActions } from 'vuex'
import eventBus from '@/components/busTag' // 引入公共的bus,來(lái)做為中間傳達(dá)的工具
export default {
props: {
title: {
type: String,
default: '提示'
},
warnMessage: {
type: String
},
redWarnMessage: {
type: String,
default: ''
},
deleteInfo: { // 刪除信息
type: Object,
default: () => {}
}
},
components: {
},
data () {
return {
deleteAdmin: false,
dialogFlag: false,
checked: false
}
},
computed: {
...mapState(['reConfirm'])
},
watch: {
reConfirm (newVal) {
this.dialogFlag = newVal
}
},
created () {
},
mounted () {
//刪除后初始化條件
eventBus.$on('deleteHandleSuccess', () => {
this.checked = false
this.deleteAdmin = false
})
},
methods: {
...mapActions(['updateReconfirm']),
closeDialog: function () {
this.checked = false
this.deleteAdmin = false
this.updateReconfirm(false)
},
exact () {
if (this.deleteInfo.adminUser && this.checked === false) {
this.deleteAdmin = true
} else {
this.$emit('deleteHandle')
}
}
}
}
</script>
<style lang="scss" scoped>
.title-reconfirm{
font-size: 18px;
}
.check-exact{
float: left;
}
.check-btn{
float: right;
}
.warn-red{
font-size: 12px;
color:red;
}
.body_message{
padding-bottom: 30px;
padding-left: 10px;
}
.warn-img{
padding-right: 30px;
display: inline-block;
width: 25px;
height:25px;
background: url("/static/icon/common-icon/warning.png") no-repeat;
border: none;
background-size: 25px 25px;
vertical-align: middle;
}
</style>
<style lang="scss">
.el-checkbox__inner{
border: 1px solid #909399;
}
.myz-info-reconfirm div.el-dialog__footer{
border:none;
text-align: right;
height: 50px;
padding: 6px 0;
}
.myz-info-reconfirm div.el-dialog__header{
border:none;
padding: 6px 0;
}
.myz-info-reconfirm div.el-dialog__body{
padding: 20px;
}
.myz-info-reconfirm div.el-dialog{
margin-top: 35vh !important;
}
/*清除浮動(dòng)-start*/
.clearfix {
*zoom: 1;
}
.clearfix:before,.clearfix:after {
display: block;
overflow: hidden;
clear: both;
height: 0;
visibility: hidden;
content: ".";
}
/*清除浮動(dòng)-end*/
</style>2.引用二次彈窗組件
<template>
<div>
<reconfirm @deleteHandle="deleteHandle" :warnMessage="'確定刪除提示'" :deleteInfo="deleteLabelInfo" :redWarnMessage="'二次提示信息'"></reconfirm>
</div>
</template>
<script>
import { mapActions } from 'vuex'
import Reconfirm from '@/components/Reconfirm'
export default {
components: {
Reconfirm
},
data () {
return {
deleteLabelInfo: {}
}
},
watch: {
},
mounted () {
},
computed: {
},
methods: {
...mapActions(['updateReconfirm']),
deleteHandle () {
//處理刪除邏輯
},
deleteAppLabel () {
//顯示二次提示條件adminUser
this.deleteLabelInfo.adminUser = (this.TeamLabelTeams.length > 0)
//顯示二次提示彈窗
this.updateReconfirm(true)
}
}
}
</script>
<style lang="scss" scoped>
</style>總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- vue中el-dialog打開(kāi)與關(guān)閉的幾種方式
- 關(guān)于Vue3中element-plus的el-dialog對(duì)話框無(wú)法顯示的問(wèn)題及解決方法
- vue+element-ui:使用el-dialog時(shí)彈框不出現(xiàn)的解決
- vue/Element?UI實(shí)現(xiàn)Element?UI?el-dialog自由拖動(dòng)功能實(shí)現(xiàn)
- vue2中如何更改el-dialog出場(chǎng)動(dòng)畫(huà)(el-dialog彈窗組件)
- Vue使用el-dialog關(guān)閉后重置表單方式
相關(guān)文章
vue動(dòng)畫(huà)效果實(shí)現(xiàn)方法示例
這篇文章主要介紹了vue動(dòng)畫(huà)效果實(shí)現(xiàn)方法,結(jié)合完整實(shí)例形式分析了vue.js+animate.css實(shí)現(xiàn)的動(dòng)畫(huà)切換效果相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
前端插件庫(kù)之vue3使用vue-codemirror插件的步驟和實(shí)例
CodeMirror是一款基于JavaScript、面向語(yǔ)言的前端代碼編輯器,下面這篇文章主要給大家介紹了關(guān)于前端插件庫(kù)之vue3使用vue-codemirror插件的步驟和實(shí)例,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
解決elementui導(dǎo)航折疊卡頓的問(wèn)題
這篇文章主要介紹了解決elementui導(dǎo)航折疊卡頓的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
webpack開(kāi)發(fā)vue-cli的項(xiàng)目實(shí)踐
本文主要介紹了webpack開(kāi)發(fā)vue-cli的項(xiàng)目實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
vue使用js-audio-recorder實(shí)現(xiàn)錄音功能
這篇文章主要為大家詳細(xì)介紹了vue如何使用js-audio-recorder實(shí)現(xiàn)錄音功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
vue項(xiàng)目配置國(guó)際化$t('')的介紹和用法示例
這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目配置國(guó)際化?$t('')的介紹和用法的相關(guān)資料,多語(yǔ)言和國(guó)際化現(xiàn)在已經(jīng)成為一個(gè)網(wǎng)站或應(yīng)用的必要功能之一,Vue作為一款流行的前端框架,在這方面也有著靈活的解決方案,需要的朋友可以參考下2023-09-09
Vue + Vite項(xiàng)目通過(guò)/dist子路徑訪問(wèn)首頁(yè)空白問(wèn)題的完整分析與解決方案
在前端工程化實(shí)踐中,將Vue應(yīng)用構(gòu)建后部署到 Nginx是一件再常見(jiàn)不過(guò)的事情,然而,當(dāng)項(xiàng)目需要以子路徑形式部署時(shí),卻經(jīng)常會(huì)遇到頁(yè)面能訪問(wèn),但首頁(yè)內(nèi)容為空白,本文將圍繞通過(guò)域名/dist/訪問(wèn) Vue+Vite項(xiàng)目首頁(yè)空白這一典型問(wèn)題,并給出一套完整、可復(fù)用的解決方案2026-01-01
Vue標(biāo)簽屬性動(dòng)態(tài)傳參并拼接字符串的操作方法
這篇文章主要介紹了Vue標(biāo)簽屬性動(dòng)態(tài)傳參并拼接字符串的操作方法,我們需要根據(jù)傳入值的類(lèi)型,在placeholder屬性賦值"請(qǐng)輸入長(zhǎng)度",“請(qǐng)輸入寬度”,"請(qǐng)輸入厚度"等提示字符,本文通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友參考下吧2023-11-11

