vue移动端模态框(可传参)的实现


Posted in Javascript onNovember 20, 2019

1-封装模态框组件Mydialog (样式可以自己写)

<template>
 <transition name="modal" tag="div">
  <div class="modal" v-show="visible" transition="fade">
   <div class="modal-dialog">
    <div class="modal-content">
     <!--头部-->
     <div class="modal-header">
      <slot name="header">
       <!-- <p class="title">{{modal.title}}</p> -->
      </slot>
      <a @click="close(0)" class="xd xd-close" href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></a>
     </div>
     <!--内容区域-->
     <div class="modal-body">
      <slot name="body">
      </slot>
     </div>
     <!--尾部,操作按钮-->
     <div class="modal-footer">
      <slot name="footer">
       <slot name="button" class="footer">>
        <a v-if="modal.showCancelButton" href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="button" :class="modal.cancelButtonClass" @click="close">{{modal.cancelButtonText}}</a>
        <a v-if="modal.showConfirmButton" href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="button" :class="modal.confirmButtonClass" @click="close()">{{modal.confirmButtonText}}</a>
       </slot>
      </slot>
     </div>
    </div>
   </div>
  </div>
  <!-- <div v-show="show" class="modal-backup"></div> -->
 </transition>
</template>

<script>
export default {
 props: {
  visible: { type: Boolean, default: false },
  options: {
   type: Object,
   default: {}
  }
 },
 // 采用v-bind将visible传入
 methods: {
  ConfirmHandler () {
   this.$emit('update:visible', false);  // 更新visible的值(可选,如果想点击确认之后,进行一些处理再决定关不关闭,可取消在这里更新visible)
   this.$emit('Confirm');  // 传递确认事件
  },
  CancelHandler () {
   this.$emit('update:visible', false);  // 更新visible的值
   this.$emit('Cancel');  // 传递取消事件
  },
  close () {
   this.visible = false;
  }
 },
 /**
  * modal 模态接口参数
  * @param {string} modal.title 模态框标题
  * @param {boolean} modal.showCancelButton 是否显示取消按钮
  * @param {string} modal.cancelButtonClass 取消按钮样式
  * @param {string} modal.cancelButtonText 取消按钮文字
  * @param {string} modal.showConfirmButton 是否显示确定按钮
  * @param {string} modal.confirmButtonClass 确定按钮样式
  * @param {string} modal.confirmButtonText 确定按钮标文字
  */
 computed: {
  /**
   * 格式化props进来的参数,对参数赋予默认值
   */
  modal () {
   let modal = this.options;
   if (modal) {
    modal = {
     title: modal.title || '提示',
     showCancelButton: typeof modal.showCancelButton == 'undefined' ? true : modal.showCancelButton,
     cancelButtonClass: modal.cancelButtonClass ? modal.showCancelButton : 'btn-default',
     cancelButtonText: modal.cancelButtonText ? modal.cancelButtonText : '取消',
     showConfirmButton: typeof modal.showConfirmButton == 'undefined' ? true : modal.cancelButtonClass,
     confirmButtonClass: modal.confirmButtonClass ? modal.confirmButtonClass : 'btn-active',
     confirmButtonText: modal.confirmButtonText ? modal.confirmButtonText : '确定',
    };
   } else { // 使用时没有传递参数
    modal = {
     title: '提示',
     showCancelButton: true,
     cancelButtonClass: 'btn-default',
     cancelButtonText: '取消',
     showConfirmButton: true,
     confirmButtonClass: 'btn-active',
     confirmButtonText: '确定',
    };
   }
   return modal;
  },
 },

}
</script>

<style lang="scss" scoped>
.modal-enter-active {
 animation: modal-in 0.35s linear;
}

.modal-leave-active {
 animation: modal-in 0.35s reverse linear;
}

@keyframes modal-in {
 0% {
  transform: translateY(-20px) rotateX(-35deg);
  opacity: 0;
 }
 50% {
  opacity: 0.5;
 }
 100% {
  transform: translateY(0) rotateX(0);
  opacity: 1;
 }
}

.modal {
 width: 100%;
 height: 100%;
 position: fixed;
 left: 0;
 top: 0;
 right: 0;
 bottom: 0;
 z-index: 1001;
 outline: 0;
 overflow: hidden;
 background-color: rgba(0, 0, 0, 0.8);
}

.modal-dialog {
 position: absolute;
 left: 50%;
 top: 50%;
 transform: translate(-50%, -50%);
 width: auto;
 width: 608px;
 background: #fff;
 border-radius: 20px;
 box-shadow: 0 8px 24px 7px rgba(0, 0, 0, 0.11);
 z-index: 1002;
 overflow: hidden;

 .modal-content {
  > div {
   // padding: 0.15rem 0.4rem;
  }
  .modal-header {
   background: url("../assets/images/tournaments/1.png") no-repeat;
   background-size: cover;
   height: 70px;
   img {
    width: 100%;
   }
  }
  .modal-body {
   // padding: 90px 0 72px 0;
   color: #3c3c43;
   font-size: 25px;
   border-bottom: 1px solid #bdbdbd;
   font-weight: 500;
  }
  .footer {
   a {
    font-size: 30px;
    color: #2c2c2c;
   }
  }
  .modal-footer {
   padding: 30px 0 40px 0;
   color: #3c3c43;
   font-size: 30px;
   font-weight: 500;
  }
 }
}

.modal-backup {
 width: 100%;
 height: 100%;
 position: fixed;
 top: 0;
 right: 0;
 bottom: 0;
 left: 0;
 z-index: 1000;
 background: rgba(0, 0, 0, 0.5);
}
</style>

2-使用方法1

页面中引入在进行调用

(1) import Mydialog from '../carrer/mydialog.vue';

(2)引入组件

components: {
  Mydialog
 }

(3  在html中插入组件

<Mydialog v-show="isShowDialog" :dialog-option="dialogOption" ref="mydialog"></Mydialog>

data中的参数

showDialog: false,
   dialogOption: {
    text: '欢迎',
    cancelButtonText: '否',
    confirmButtonText: '是',
    isShowCancelButton: ''
  },

methods中 在需要出现弹框时候让其显示就好啦

showDialog()
   this.dialogOption.text = ` <p>确认拒绝?</p> `;
   this.dialogOption.isShowCancelButton = true
   this.showDialog = true;
   this.$refs.mydialog.confirm().then(() => {
    this.showDialog = false;
    this.refuse(id)
   }).catch(() => {
    this.showDialog = false;
   })
  },

3.使用方法2

因为在项目中经常要使用到,而且每次使用的时候都要带上相同的参数,所以就封装了一个js,(模态框的工具类),使用的时候调用就好了

1)- 新建一个js文件dialogUtil,复制下面的代码就好了

class normalDialog {
 constructor(args) {
  // console.log("args",args);
  this.parent = args.parent;
  this.isShowDialog = args.isShowDialog;
  this.dialogOption = this.parent[args.dialogOption];
  this.mydialog = this.parent.$refs[args.mydialog];
  // console.log("dialogOption=",this.dialogOption);
 }

 showDialog(text, showCancelButton, success, error) {
  console.log("showDialog=="+text);
  this.dialogOption.text = text || "请输入弹框标题";
  let suc = typeof showCancelButton == "function" ? showCancelButton : success;
  let err = typeof showCancelButton == "function" ? success : error;
  if (typeof showCancelButton != "function") {
   this.dialogOption.isShowCancelButton = !!showCancelButton;
  } else {
   this.dialogOption.isShowCancelButton = true;
  }
  this.parent[this.isShowDialog] = true;
  this.confirm(suc, err);
 }

 //弹框回调
 confirm(success, error) {
  let self = this;
  this.mydialog.confirm().then(() => {
   typeof success == "function" && success();
   self.parent[this.isShowDialog] = false;
  }).catch(() => {
   typeof error == "function" && error();
   self.parent[this.isShowDialog] = false;
  })
 }

 toString() {
  // console.log(this.name + " : " + this.age);
 }

}

export default {
 //args:参数, 类型
 creatByType(args, type) {
  type = !!type ? type : 1;

  switch (type) {
   case 1:
    return new normalDialog(args)
    break;
   case 2:
    break
   default:
    break;
  }
 }
}

2) 因为很多页面都用到,所以让他成为全局的(在main中引入就好了),那么别的页面使用就不需要引入了

import dbDiaLogUtil from './assets/js/dialogUtil'
Vue.prototype.$dbDiaLogUtil = dbDiaLogUtil;

3)在使用的时候

页面中引入组件在进行调用

(1) import Mydialog from '../carrer/mydialog.vue';

(2)引入组件

components: {
  Mydialog
 }

 (3) 在html中插入组件

<Mydialog v-show="isShowDialog" :dialog-option="dialogOption" ref="mydialog"></Mydialog>

在data()中用对象的形式

isShowDialog : false,
   dialogOption:{text: '',cancelButtonText: '否',confirmButtonText: '确认',isShowCancelButton: false},
   dialogNormal:null,

在mounted中进行初始化

this.dialogNormal = this.$dbDiaLogUtil.creatByType({parent:this,isShowDialog:'isShowDialog',dialogOption:'dialogOption',mydialog:'mydialog'});

最后在需要弹框的地方调用,一句代码搞定啦

this.dialogNormal.showDialog('dialog要显示的内容',function(){console.log('成功执行的')} , function(){console.log('失败执行的')})

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
农历与西历对照
Sep 06 Javascript
测试JavaScript字符串处理性能的代码
Dec 07 Javascript
Juqery Html(),append()等方法的Bug解决方法
Dec 13 Javascript
jQuery代码优化 事件委托篇
Nov 01 Javascript
使用JavaScript判断图片是否加载完成的三种实现方式
May 04 Javascript
在其他地方你学不到的jQuery小贴士和技巧(欢迎收藏)
Jan 20 Javascript
基于jQuery实现点击列表加载更多效果
May 31 Javascript
js实现适合新闻类图片的轮播效果
Feb 05 Javascript
JavaScript实现获取远程的html到当前页面中
Mar 26 Javascript
Vue-cli-webpack搭建斗鱼直播步骤详解
Nov 17 Javascript
js如何获取访问IP、地区、当前操作浏览器
Jul 23 Javascript
vue设置全局访问接口API地址操作
Aug 14 Javascript
微信小程序实现上拉加载功能
Nov 20 #Javascript
微信小程序实现锚点功能
Nov 20 #Javascript
vue实现element表格里表头信息提示功能(推荐)
Nov 20 #Javascript
微信小程序实现时间进度条功能
Nov 17 #Javascript
小程序实现投票进度条
Nov 20 #Javascript
javascript跳转与返回和刷新页面的实例代码
Nov 20 #Javascript
vant IndexBar实现的城市列表的示例代码
Nov 20 #Javascript
You might like
php数组中删除元素的实现代码
2012/06/22 PHP
php中ob_get_length缓冲与获取缓冲长度实例
2014/11/20 PHP
Nigma vs AM BO3 第一场2.13
2021/03/10 DOTA
JavaScript 工具库 Cloudgamer JavaScript Library v0.1 发布
2009/10/29 Javascript
js 创建快捷方式的代码(fso)
2010/11/19 Javascript
十个迅速提升JQuery性能让你的JQuery跑得更快
2012/12/10 Javascript
js复制到剪切板的实例方法
2013/06/28 Javascript
jQuery.extend()、jQuery.fn.extend()扩展方法示例详解
2014/05/08 Javascript
利用jQuery及AJAX技术定时更新GridView的某一列数据
2015/12/04 Javascript
Vuejs第九篇之组件作用域及props数据传递实例详解
2016/09/05 Javascript
NodeJS实现微信公众号关注后自动回复功能
2017/05/31 NodeJs
AngularJS实现的select二级联动下拉菜单功能示例
2017/10/25 Javascript
JS实现小球的弹性碰撞效果
2017/11/11 Javascript
深入理解JavaScript的值传递和引用传递
2018/10/24 Javascript
JS实现随机生成10个手机号的方法示例
2018/12/07 Javascript
6行代码实现微信小程序页面返回顶部效果
2018/12/28 Javascript
基于Vue2-Calendar改进的日历组件(含中文使用说明)
2019/04/14 Javascript
详解vue-router 动态路由下子页面多页共活的解决方案
2019/12/22 Javascript
[47:08]OG vs INfamous 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/17 DOTA
python 获取et和excel的版本号
2009/04/09 Python
Python面向对象特殊成员
2017/04/24 Python
Pycharm中切换pytorch的环境和配置的教程详解
2020/03/13 Python
基于Pytorch版yolov5的滑块验证码破解思路详解
2021/02/25 Python
爱他美官方海外旗舰店:Aptamil奶粉
2017/12/22 全球购物
物业管理公司实习生自我鉴定
2013/09/19 职场文书
大学自我鉴定
2013/12/20 职场文书
博士学位自我鉴定范文
2013/12/26 职场文书
2014年中秋节活动总结
2014/08/29 职场文书
怀孕辞职信怎么写
2015/02/28 职场文书
幼儿园安全教育月活动总结
2015/05/08 职场文书
幼儿园校车安全责任书
2015/05/08 职场文书
村主任当选感言
2015/08/01 职场文书
2016年社区综治宣传月活动总结
2016/03/16 职场文书
pycharm 如何查看某一函数源码的快捷键
2021/05/12 Python
关于antd tree 和父子组件之间的传值问题(react 总结)
2021/06/02 Javascript
Tomcat弱口令复现及利用
2022/05/06 Servers