<template>
<transition name='fade'>
<div class="alert" v-if="showAlert">
<div class="wrap">
<div class="head">{{title}}</div>
<div class="body">
<slot>
<p>{{message}}</p>
</slot>
</div>
<div class="foot">
<div v-if="type === 'confirm'">
<button class="btn-base" @click="sure">确定</button>
<button class="btn-gray" @click="cancel">取消</button>
</div>
<div v-else-if="type === 'inform'">
<button class="btn-base" @click="cancel">知道了</button>
</div>
</div>
</div>
</div>
</transition>
</template>
<script>
export default {
name: 'alert',
data() {
return {
showAlert: false,
};
},
props: {
title: {
type: String,
default: '提示',
},
message: {
type: String,
},
type: { // 可以有confirm, 和inform两个类型
type: String,
default: 'confirm',
validator(value) {
return value === 'confirm' || value === 'inform';
},
},
sureBtn: {
type: Function,
},
cancelBtn: {
type: Function,
},
context: {
type: Object,
},
},
methods: {
cancel() {
if (this.cancelBtn) {
this.cancelBtn.apply(this.context);
}
this.close();
},
sure() {
if (this.sureBtn) {
this.sureBtn.apply(this.context);
}
this.close();
},
show() {
this.showAlert = true;
},
close() {
this.showAlert = false;
},
},
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang='scss'>
.alert {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0,0,0, 0.8);
z-index: 1000;
transition: all .3s ease-in-out;
}
.wrap {
position: absolute;
z-index: 1002;
min-width: 400px;
background: #fff;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
border-radius: 4px;
}
.head {
height: 40px;
line-height: 40px;
border-bottom: 1px solid #dedede;
padding-left: 10px;
color: #333;
}
.body {
padding: 40px 20px;
text-align: center;
}
.foot {
height: 50px;
text-align: center;
button {
margin-right: 20px;
&:last-child {
margin-right: 0;
}
}
}
</style>
<!--template-->
<button @click="showAlert">点我</button>
<alert ref="alert">我是一个确认框</alert>
<!--javascript-->
...
methods: {
showAlert() {
this.$refs.alert.show();
}
}
...
import Vue from 'vue';
import alert from './alert';
const AlertConstructor = Vue.extend(alert);
const div = document.createElement('div');
AlertConstructor.show = (options) => {
document.body.appendChild(div);
options.type = 'inform';
const propsData = Object.assign({}, options);
const alertInstance = new AlertConstructor({
propsData,
}).$mount(div);
alertInstance.show();
};
AlertConstructor.confirm = (options) => {
document.body.appendChild(div);
options.type = 'confirm';
const propsData = Object.assign({}, options);
const alertInstance = new AlertConstructor({
propsData,
}).$mount(div);
alertInstance.show();
};
export default AlertConstructor;
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有