components 通用组件文件夹下 创建 message1文件夹
创建 index.js js文件
import Vue from 'vue';
import Confirm from './index.vue'; // 引入组件
let newInstance;
const ConfirmInstance = Vue.extend(Confirm); // 创建构造函数
const initInstance = () => { // 执行方法后完成挂载
newInstance = new ConfirmInstance(); // 实例化
document.body.appendChild(newInstance.$mount().$el);
// 实例化后手动挂载,得到$el真实Dom,将其添加到body最后
}
export default options => {
//导出一个方法,接受配置参数
if (!newInstance) {
initInstance(); // 挂载
}
Object.assign(newInstance, options);
// 实例化后newInstance就是一个对象了,所以data内的数据会
// 挂载到this下,传入一个对象与之合并
return newInstance.show(vm => { // 显示弹窗
newInstance = null; // 将实例对象清空
})
}
message1文件夹下创建组件 index.vue
<template>
<transition name="confirm-fade">
<div class="confirm" v-show="showFlag">
<div class="confirm-wrapper">
<div class="confirm-content">
<div class="text">{{title}}</div>
<div class="operate" @click.stop>
<div class="operate-btn left" @click="cancel">{{cancelBtnText}}</div>
<div class="operate-btn" @click="confirm">{{ConfirmBtnText}}</div>
</div>
</div>
</div>
</div>
</transition>
</template>
<script>
export default {
data() {
return {
showFlag: false,
title: "确认清空所有历史纪录吗?",
ConfirmBtnText: "确定",
cancelBtnText: "取消"
};
},
methods: {
show(cb) {
this.showFlag = true;
typeof cb === "function" && cb.call(this, this);
return new Promise((resolve, reject) => {
this.reject = reject;
this.resolve = resolve;
});
},
cancel() {
this.reject("cancel");
this.hide();
},
confirm() {
this.resolve("confirm");
this.hide();
},
hide() {
this.showFlag = false;
document.body.removeChild(this.$el);
this.$destroy();
}
}
};
</script>
<style lang="scss" scoped>
.confirm {
position: fixed;
left: 0;
right: 0;
top: 0;
bottom: 0;
z-index: 998;
background-color: rgba(0, 0, 0, 0.3);
&.confirm-fade-enter-active {
animation: confirm-fadein 0.3s;
.confirm-content {
animation: confirm-zoom 0.3s;
}
}
.confirm-wrapper {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 999;
.confirm-content {
width: 550px;
border-radius: 13px;
background: #fff;
.text {
padding: 80px 15px;
line-height: 60px;
text-align: center;
color:#000;
font-size: 45px;
}
.operate {
display: flex;
align-items: center;
text-align: center;
font-size: 18px;
.operate-btn {
color:#000;
flex: 1;
line-height: 22px;
font-size: 40px;
border-top: 1px solid rgba(0, 0, 0, 0.3);
padding: 40px 0;
&.left {
border-right: 1px solid rgba(0, 0, 0, 0.3);
}
}
}
}
}
}
@keyframes confirm-fadein {
0% {opacity: 0;}
100% {opacity: 1;}
}
@keyframes confirm-zoom {
0% {transform: scale(0);}
50% {transform: scale(1.1);}
100% {transform: scale(1);}
}
</style>
在main.js 注册组件
import EditionTipS from '@/components/message1/index.js' // 路径
Vue.prototype.$EditionTipS = EditionTipS
//这样调用吧~
this.$Confirm({
title: 'vue永存'
}).then(confirm => {
console.log(confirm)
}).catch(cancel => {
console.log(cancel)
})