vue彈出框組件封裝實(shí)例代碼
新學(xué)vue,參考別人封裝彈出層組件。好用!
1.你需要先建一個(gè)彈出框的模板:
//首先創(chuàng)建一個(gè)mack.vue
<template>
<div class="mack" v-if="isShow">
<div class="mackWeb" :style="text.mackStyle">
<div class="title font_b" v-if="text.title" :style="text.titleStyle">{{text.title.trim()}}</div>
<div class="mesg font_s" v-if="text.mesg" :style="text.mesgStyle">{{text.mesg.trim()}}</div>
<div v-html="text.cntMsg"></div>
<div class="footb font_s">
<div
class="foot_l borderlf borderTop"
@click="cancel"
v-if="text.cancel"
:style="text.cancelValStyle"
>{{text.btn.cancelVal}}</div>
<div
class="foot_r borderTop"
@click="confirm"
v-if="text.confirm"
:style="text.confirmValStyle"
>{{text.btn.confirmVal}}</div>
</div>
</div>
</div>
</template>
//寫(xiě)js
<script>
export default {
data() {
return {
isShow: true,
text: {
title: "",
mesg: "",
cnTmesg: "",
cancel: true,
confirm: true,
mackStyle: null,
titleStyle: null,
mesgStyle:null,
cancelValStyle: null,
confirmValStyle: null,
btn: {
confirmVal: "確定",
cancelVal: "取消"
}
}
};
},
methods: {
cancel() {
this.isShow = false;
},
confirm() {
this.isShow = false;
}
}
};
</script>
//css
<style scoped lang='less'>
.mack {
position: fixed;
width: 100%;
height: 100%;
overflow: hidden;
top: 0;
left: 0;
background: rgba(21, 21, 21, 0.7);
.font_b {
font-size: 14/50rem;
color: #231a2f;
}
.font_s {
font-size: 12/50rem;
color: #655a72;
}
.borderTop {
border-top: 1/50rem solid #e4e4e4;
}
.mackWeb {
background: #ffffff;
width: 300/50rem;
min-width: 300/50rem;
margin: auto;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
.title {
text-align: center;
padding: 15/50rem 15/50rem 0 15/50rem;
}
.mesg {
padding: 15/50rem;
text-align: center;
}
.footb {
display: inline-table;
width: 100%;
.borderlf {
border-right: 1/50rem solid #e4e4e4;
}
div {
display: table-cell;
box-sizing: border-box;
text-align: center;
padding: 10/50rem 0;
}
}
}
}
</style>
2.接著你需要一個(gè)js:mackjs.js
import Vue from 'vue';
import confirm from './mack';
let confirmConstructor = Vue.extend(confirm);
let theConfirm = function (text) {
return new Promise((res, rej) => { //promise封裝,ok執(zhí)行resolve,no執(zhí)行rejectlet
let confirmDom = new confirmConstructor({
el: document.createElement('div')
})
document.body.appendChild(confirmDom.$el); //new一個(gè)對(duì)象,然后插入body里面
confirmDom.text = Object.assing( confirmDom.text,text); //為了使confirm的擴(kuò)展性更強(qiáng),這個(gè)采用對(duì)象的方式傳入,所有的字段都可以根據(jù)需求自定義
confirmDom.ok = function () {
res()
confirmDom.isShow = false
}
confirmDom.close = function () {
rej()
confirmDom.isShow = false
}
})
}
export default theConfirm;
//暴露出去,別忘記掛載到vue的原型上
// => 在main.js里面先引入 import theConfirm from './components/confirm/confirm.js'
// => 再掛載 Vue.prototype.$confirm = theConfirm;
//在需要的地方直接用以下方法調(diào)用即可:
// this.$confirm({
// type:'提示',
// msg:'是否刪除這條信息?',
// btn:{
// ok:'yes',
// no:'no'
// }
// }).then(() => {
// console.log('ok')
// })
// .catch(() => {
// console.log('no')
// })
-3.你接著需要在main.js導(dǎo)入這個(gè)文件
import macksjs from './assets/mackjs' Vue.prototype.$confirm= macksjs ;
-4.最后在你需要引入的vue文件中直接調(diào)用就好了
<button @click="deleteaas">我是彈出框</button>
methods:{
deleteaas() {
this.$confirm({
title: "addd",
mesg: "您確定不再關(guān)注該客戶嗎?",
cntMsg: '<div class="helAA">你好</div>',
cancelValStyle:{color:'red'},
btn: {
confirmVal: "確a定",
cancelVal: "取a消"
}
})
.then(() => {
console.log("yes");
//點(diǎn)擊確定之后的處理
})
.catch(() => {
console.log("no");
});
}
}
導(dǎo)入
總結(jié)
以上所述是小編給大家介紹的vue彈出框組件封裝實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
Mock.js在Vue項(xiàng)目中的使用小結(jié)
這篇文章主要介紹了Mock.js在Vue項(xiàng)目中的使用,在vue.config.js中配置devServer,在before屬性中引入接口路由函數(shù),詳細(xì)步驟跟隨小編通過(guò)本文學(xué)習(xí)吧2022-07-07
淺談ElementUI中switch回調(diào)函數(shù)change的參數(shù)問(wèn)題
今天小編就為大家分享一篇淺談ElementUI中switch回調(diào)函數(shù)change的參數(shù)問(wèn)題,具有很好的價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
關(guān)于vue.extend的使用及說(shuō)明
這篇文章主要介紹了關(guān)于vue.extend的使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
vue webpack多頁(yè)面構(gòu)建的實(shí)例代碼
這篇文章主要介紹了vue webpack多頁(yè)面構(gòu)建的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2018-09-09
關(guān)于Vue Router中路由守衛(wèi)的應(yīng)用及在全局導(dǎo)航守衛(wèi)中檢查元字段的方法
這篇文章主要介紹了關(guān)于Vue Router中路由守衛(wèi)的應(yīng)用及在全局導(dǎo)航守衛(wèi)中檢查元字段的方法,實(shí)現(xiàn)方法有兩種,本文通過(guò)實(shí)例代碼對(duì)每種方法介紹的很詳細(xì),需要的朋友參考下2018-12-12
Vue腳手架配置代理服務(wù)器的兩種方式(小結(jié))
本文主要介紹了Vue腳手架配置代理服務(wù)器的兩種方式(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
Vue打包優(yōu)化之生產(chǎn)環(huán)境刪除console日志配置
這篇文章主要為大家介紹了Vue打包優(yōu)化之生產(chǎn)環(huán)境刪除console日志配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
詳解vue修改elementUI的分頁(yè)組件視圖沒(méi)更新問(wèn)題
這篇文章主要介紹了詳解vue修改elementUI的分頁(yè)組件視圖沒(méi)更新問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

