一、在实际的开发当中,弹窗是少不了的,默认系统的弹窗样式太丑,难以满足项目的实际需求,所以需要自己定义弹窗组件,把弹窗组价定义为全局的,这样减少每次使用的时候引入麻烦,节省开发时间。本文将分享如何定义一个全局的弹窗组件。下边开始上代码。

二、实际代码如下:

1.在components目录下的public目录新建一个文件夹alert,然后新建两个文件alert.vue和alert.scss。组件的样式代码我喜欢跟组件放到一起,这样按模块去划分管理。公共的样式就放到公共的样式文件里就行了。

2.alert.vue代码如下

<template>
<div class="alertModal" ref="alert">
<!--social post弹框-->
<transition name="fade">
<div v-if="modelFlag==1" class="alertbox">
<div class="alert-dialog">
<div class="alert-content">
<div class="alert-header">
<span class="alertclose" @click="close">×</span>
<span class="alert-title">
{{modelTitle}}
</span>
</div>
<div class="alert-body">
{{modelContent}}
</div>
<div class="alert-footer">
<button class="alertbtn" @click="close">{{modelClose}}</button>
<button class="alertbtn alert-info" @click="submit">{{modelSave}}</button>
</div>
</div>
</div>
</div>
</transition>
<div v-if="modelFlag==1" class="modal-backdrop"></div>
</div>
</template> <script> export default {
data(){
return{
modelFlag:0,//0为消失,1为显示
modelTitle:'Alert',//弹窗标题
modelClose:'取消',//取消按钮文字
modelContent:'',//弹窗内容
modelSave:'确定',//确定按钮文字
callBack:null,//是否需要回调函数
}
},
methods:{
//回调函数
doCallBack(){
if(typeof this.callBack == 'function'){
this.callBack()
this.callBack=null;
}
},
//关闭弹窗,数据重置
close(){
this.modelFlag=0;
this.modelTitle='Alert';
this.modelClose='取消';
this.modelContent='';
this.modelSave='确定';
this.callBack=null;
},
//点击确定按钮弹窗消失
submit(){
this.doCallBack()
this.close()
},
//显示弹窗,记性复制
show(options){
if(this.modelFlag==1){return};
this.modelTitle=options.modelTitle||this.modelTitle;
this.modelContent=options.modelContent;
this.modelFlag=1;
this.modelSave=options.modelSave||this.modelSave;
this.modelClose=options.modelClose||this.modelClose;
if(options.callBack){
this.callBack=options.callBack;
}
},
},
watch:{ }
}
</script> <!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="sass" scoped>
@import 'alert.scss'
</style>

3.在App.vue中引入组件并注册,

import alert from 'components/public/alert/alert.vue'
mounted(){
Vue.prototype.$alert=this.$refs.alert;
},
components:{
alert
}
<alert ref='alert'></alert>

在外层div下加上组件。

4.使用弹窗

比如我在一个页面那里点击一个button然后调用显示这弹窗,则:

<button @click="showalert">show alert</button>
methods:{
showalert(){
this.$alert.show({modelTitle:"Alert Msg",
modelContent:'Please Check!'})
}
},

this.$alert.show({modelTitle:"Alert Msg",modelContent:'Please Check!'}),show方法里边传一个对象,里边是相应的配置。

这样就可以使用啦!

5.使用confirm功能,在对象里加入callBack回调函数:

showalert(){
this.$alert.show({modelTitle:"Alert Msg",modelContent:'你确定删除吗?',callBack:()=>{
alert(1)
}})
}

结果如下:

6.最后附上样式代码

.modal.fade .alert-dialog {
-webkit-transition: -webkit-transform .3s ease-out;
-o-transition: -o-transform .3s ease-out;
transition: transform .3s ease-out;
-webkit-transform: translate(0, -25%);
-ms-transform: translate(0, -25%);
-o-transform: translate(0, -25%);
transform: translate(0, -25%);
}
.modal.in .alert-dialog {
-webkit-transform: translate(0, 0);
-ms-transform: translate(0, 0);
-o-transform: translate(0, 0);
transform: translate(0, 0);
}
.alertbox{
position: fixed;
top:;
bottom:;
left:;
right:;
text-align: center;
z-index:;
}
.alert-dialog{
display: inline-block;
width: 420px;
padding-bottom: 10px;
vertical-align: middle;
background-color: #fff;
border-radius: 4px;
border: 1px solid #e6ebf5;
font-size: 18px;
box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
text-align: left;
overflow: hidden;
backface-visibility: hidden;
position: relative;
top: 140px;
padding: 10px 15px;
}
.modal-backdrop.fade {
filter: alpha(opacity=0);
opacity:;
}
.modal-backdrop.in {
filter: alpha(opacity=50);
opacity: .5;
}
.alert-footer{
float: right;
margin-top: 5px;
}
.alert-scrollbar-measure {
position: absolute;
top: -9999px;
width: 50px;
height: 50px;
overflow: scroll;
} .fade-enter-active, .fade-leave-active {
transition: opacity .5s
}
.fade-enter, .fade-leave-to /* .fade-leave-active in <2.1.8 */ {
opacity: 0
}
.modal-backdrop {
position: fixed;
top:;
right:;
bottom:;
left:;
z-index:;
background-color: #000;
opacity: 0.5;
} .el-icon-date{
cursor: pointer;
}
.alert-header{ }
.alert-title{
font-size: 18px;
line-height:;
color: #2d2f33;
}
.alert-body{
padding: 10px 0px;
color: #5a5e66;
font-size: 14px;
line-height: 17px;
}
.alertbtn{
text-align: center;
font-weight:;
cursor: pointer;
padding: 9px 15px;
font-size: 12px;
border-radius: 3px;
line-height:;
background: #fff;
border: 1px solid #d8dce5;
border-color: #d8dce5;
color: #5a5e66;
}
.alert-info{
color: #fff;
background-color: #409eff;
border-color: #409eff;
}
.alertclose{
float: right;
cursor: pointer;
}

希望对大家有用。

用vue2.x注册一个全局的弹窗alert组件、confirm组件的更多相关文章

  1. 用vue2.x注册一个全局的弹窗alert组件

    一.在实际的开发当中,弹窗是少不了的,默认系统的弹窗样式太丑,难以满足项目的实际需求,所以需要自己定义弹窗组件,把弹窗组价定义为全局的,这样减少每次使用的时候引入麻烦,节省开发时间.本文将分享如何定义 ...

  2. vue中封装一个全局的弹窗js

    /** * Created by yx on 2017/12/21. */ export default { /** * 带按钮的弹框 * <!--自定义提示标题,内容,单个按钮事件--> ...

  3. Vue全局弹窗:一次注册,全局可弹

    Vue全局弹窗 今天来搞一个全局弹窗,不用每个文件都引入,只在main.js里作为全局原型引入就好了 先新建弹窗组件 toast.vue <template></template&g ...

  4. vue插件 使用use注册Vue全局组件和全局指令

    插件一般会注册到全局使用 官方编辑插件介绍:https://vuefe.cn/v2/guide/plugins.html 全局组件: .首先建一个自定义组件的文件夹,比如叫loading,里面有一个i ...

  5. 在Form Load中设置showInTaskBar =false 或 隐藏窗口 this.Hide()时会导致注册的全局快捷键无效

    在Form Load中设置showInTaskBar =false   或 隐藏窗口 this.Hide() 会导致注册的全局快捷键无效.  反正是其中一个,有点记不清了. 在Form Shown中s ...

  6. 【Head First Servlets and JSP】笔记7:如何创建一个全局的dog?

    重定向与请求分派 “局部”参数——ServletConfig——servlet初始化参数 “全局”参数——ServletContext——上下文初始化参数 Web app的“构造器”——Servlet ...

  7. 使用gacutil把COM组件注册到全局缓存GAC中

    我们在编写软件的时候,有时候需要调用COM组件,那就需要注册了,注册有两种,一种是使用regasm 在程序运行的时候注册,参考“pb调用C#编写的DLL类库“,不过受路径的限制.还有一种注册方式,使用 ...

  8. mappers:将sql映射注册到全局配置中

    <!-- 将我们写好的sql映射文件(EmployeeMapper.xml)一定要注册到全局配置文件(mybatis-config.xml)中 --> <!-- 6.mappers: ...

  9. vue 复习篇. 注册全局组件,和 组件库

    初篇 ============================================================== 1. 编写loading组件(components/Loading/ ...

随机推荐

  1. SpringMVC中的 --- 异常处理

    系统异常处理器SimpleMappingExceptionResolver 处理器方法执行过程中,可能会发生异常,不想看到错误黄页,想看到一个友好的错误提示页. 自定义异常处理器 使用异常处理注解

  2. iOS-UIWebview比例缩放

    你在使用UIWebview显示网页时.可能会注意到.UIWebView所支持的缩放倍率是非常有限的.而在Safari自己所支持的缩放系数比UIWebview要大得多. 本文解释了怎样加大UIWebVi ...

  3. Caused by: java.lang.UnsatisfiedLinkError: Couldn&#39;t load BaiduMapVOS_v2_1_3: findLibrary returned nu

    在使用百度地图进行开发的时候.假设遇到了 Caused by: java.lang.UnsatisfiedLinkError: Couldn't load BaiduMapVOS_v2_1_3: fi ...

  4. Git 少用 Pull 多用 Fetch 和 Merge 【已翻译100%】【转】

    本文转载自:https://www.oschina.net/translate/git-fetch-and-merge?lang=chs&page=1# 本文有点长而且有点乱,但就像Mark ...

  5. bzoj4950: [Wf2017]Mission Improbable

    跟着靖靖做题%%%%% 这题一看就觉得和之前的某场模拟赛的一道题很像,找假如某行某列的最大值一样的就可以只堆一个,跑匈牙利就行 一开始以为箱子不能移动-_-! 然后有个坑,大家都知道当这个位置有箱子就 ...

  6. Java插入中文到数据库中文变成问号解决

    转自:https://blog.csdn.net/hellosweet1/article/details/81673152 之所以会出现乱码,就是编码方式不一致导致的 我们应该首先确定         ...

  7. Django day08 多表操作 (三) 基于对象的跨表查询 基于双下划线的多表查询

    一: 基于对象的跨表查询 1. 一对一 正向: 反向: 2. 一对多 正向: 反向: 3.多对多 正向: 反向: 4.*****基于对象的多表查询 二: 基于双下划线的多表查询 1. 连表查询 一对一 ...

  8. 用xftp从win7系统传输一些必要的文件到Linux

    新建会话,主机名为Linux系统的ip地址,选用SFTP协议,选用UTF-8编码格式 1.安装JDK 切换到java路径下 卸载openJDK: 用rpm -qa |grep java指令查看 用rp ...

  9. [BZOJ1307][ZJOI2008]生日聚会PARTY

    ...一开始用了三维的...甚至尝试把它搞成二维的...后来发现根本没法转移呀... 既然dalao说这是初中题,那它就算是一道初中题吧... dp[i][j][k][p]表示当前有i个男生j个女生, ...

  10. [CodeForces522B] Photo to Remember

    某一天,n个朋友在一起聚会,他们已经很久没见了,于是他们决定拍照留念. 简单的说,拍照的时候,每个人有一个高度和宽度,第i个的高度和宽度分别是hi和wi.这些人排成一条直线,照片的最小的面积必须包含所 ...