详解如何用VUE写一个多用模态框组件模版


Posted in Javascript onSeptember 27, 2018

对于新手们来说,如何写一个可以多用的组件,还是有点难度的,组件如何重用,如何传值这些在实际使用中,是多少会存在一些障碍的,所以今天特意写一个最常用的模态框组件提供给大家,希望能帮助到您!

懒癌患者直接复制粘贴即可

Modal.vue组件

<template>
  <!-- 过渡动画 -->
  <transition name="modal-fade">
    <!-- 关闭模态框事件 和 控制模态框是否显示 -->
    <div class="modal-backdrop" @click="$emit('closeModal')" v-show="show">
      <div class="modal">
        <img class="img" :src="imgadress" alt="">
        <div class="modal-body" id="modalDescription">
          <li></li>
          <!-- 状态提示文字的插槽 -->
          <slot name="status">{{statusText}}</slot>
          <li></li>
        </div>
        <!-- 模态框内容文字 可有可无 -->
        <div class="modal-content" v-if="contentText">
          {{contentText}}
          <span v-if="IDList" v-for="item in IDList" :key="item.id">{{item.payMoney}}
            <i>{{item.yuan}}</i>
          </span>
        </div>
        <ul>
          <!-- 模态框按钮 可选单个确定按钮 和 两个确定、取消按钮 -->
          <!-- 单个确定按钮 -->
          <li v-if="alert" :class="buttonBackground" @click.stop="$emit('button')">确定</li>
          <!-- 确定和取消按钮 -->
          <li v-else class="confirm">
            <div>取消</div>
            <div :class="buttonBackground" @click.stop="$emit('confirm')">{{sure}}</div>
          </li>
        </ul>
      </div>
    </div>
  </transition>
</template>
<script>
export default {
  name:'modal',
  // 通过 props 传值
  props: {
    imgadress:String,
    title:String, //标题文字
    show:{   //显示取消
      type:Boolean,
      default:false
    },
    statusText:String,  //状态文字
    contentText:String,  //描述文字
    IDList:Array,  //ID 列表
    payMoney:Number,
    yuan:String,
    buttonBackground:String, //按钮背景色
    alert:String,  //判断一个还是两个按钮
    sure:String, //第二个按钮的提示文字
    
  },
  data(){
    return{
      closemodal:"close",
      // isModalVisible:false,
      // 确定和取消按钮的两种颜色
      red:'redBackground',
      blue:'blueBackground'
    }
  },
  methods:{
    // 关闭模态框事件
    close(){
      this.$emit('close')
    },
  }
}
</script>
<style lang="scss" scoped> 
.modal-backdrop {
  position: fixed; 
  top: 0; 
  right: 0; 
  bottom: 0; 
  left: 0; 
  background-color: rgba(0,0,0,.3); 
  display: flex; 
  justify-content: center; 
  align-items: center;
  z-index: 12;
  .modal { 
    background-color: #fff; 
    box-shadow: 2px 2px 20px 1px; 
    overflow-x:auto; 
    display: flex; 
    flex-direction: column; 
    width: 11.8rem;
    position: relative;
    border-radius: 0.2rem;
    .img{
      width: 3.6rem;
      height: 3.6rem;
      margin: 0.8rem 4.1rem;
    }
    .modal-header{ 
      padding: 0.6rem 4.1rem;
      width: 3.6rem;
      height: 3.6rem;
      box-sizing: border-box; 
      .img{
        width: 100%;
        height: 100%;
      }
      div{
        width: 100%;
        height: 100%;
        background: #000;
      }
    } 
    .modal-body{
      width: 100%;
      height: 0.48rem;
      padding: 0rem 1.6rem;
      margin-bottom: 0.8rem;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between; 
      align-items: center; 
      li{
        width: 2rem;
        height: 0.04rem;
        background: #eeeee5;
      }
    }
    .modal-content{
      width: 100%;
      // height: 0.6rem;
      margin-bottom: 0.8rem;
      text-align: center;
      color: #34304B;
      span{
        color: #32B8B9;
        i{
          color: #4F4F4F;
        }
      }
    }
    ul{
      li{
        width: 100%;
        height: 1.52rem;
        line-height: 1.52rem;
        text-align: center;
        color: #fff;
        font-size: 0.56rem;
        letter-spacing: 0.4rem;
      }
      .confirm{
        display: flex;
        div:nth-child(1){
          flex: 1;
          background: #DEDEDE;
          color: #BCBBBF;
        }
        div:nth-child(2){
          flex: 1;
          color: #fff;
        }
      }
    }
    .blueBackground{
      background: #21A6DF;
    }
    .redBackground{
      background: #FF4046;
    }
  } 
}
/* 动画 */
.modal-fade-enter,.modal-fade-leave-active{
  opacity: 0;
}
.modal-fade-enter-active, .modal-fade-leave-active{
  transition: opacity 0.5s ease;
}
</style>

新建一个index.js文件,在其中全局引入组件,全局引入之后,就不用在每个调用的组件里面单独引入了,可以直接使用

import Modalfrom "./Modal.vue";
const components = {
  install: function (Vue) {
    Vue.component('Modal', Modal);
  }
}
//导出组件
export default components;

Index.vue中调用

<template>
  <div class="index">
<!-- 提交成功模态框 -->
    <Modal
      ref="Modal"
      :imgadress="imgadress"
      v-show="isModalVisible"
      statusText="提交成功"
      @closeModal="closeModal"
      contentText="Index.vue"
      :IDList="IDList"
      :buttonBackground="red"
      sure="确定"
      @confirm="confirm"
    >
      <!-- :payMoney="payMoney"
      yuan="元" -->
    </Modal>
    <button @click="showModel">支付成功模态框</button>

  </div>
</template>
<script>
export default {
  name: 'Index',
  data(){
    return{
      // 模态框
      imgadress:require('./../../assets/img/success.png'),
      isModalVisible:false,
      show: false,
      showToast: false,
      thisIndex:0,
      green:'green',
      blue:'',
      red:'',
      IDList:[
        {payMoney:23456,yuan:'、'},
        {payMoney:23456,yuan:'、'},
        {payMoney:23456,yuan:'、'},
        {payMoney:23456,yuan:'、'},
        {payMoney:23456,yuan:'、'},
        {payMoney:23456,yuan:'、'},
        {payMoney:23456,yuan:'、'},
      ],
      payMoney:99,
    }
  },
  methods:{
    // 提示模态框
    showModel(){
      this.isModalVisible = true;
      this.blue = this.$refs.Model.blue
      this.red = this.$refs.Model.red
      
    },
    closeModal(){
      this.isModalVisible = false
    },
    confirm(){
      console.log(11111111111);
    },
  }
}
</script>

效果如图

详解如何用VUE写一个多用模态框组件模版

模态框-1.gif

如果只需要一个确定按钮,只需要在调用的时候,这么写就好了

<template>
  <div class="index">
<!-- 提交成功模态框 -->
    <Modal
      ref="Modal"
      :imgadress="imgadress"
      v-show="isModalVisible"
      statusText="提交成功"
      @closeModal="closeModal"
      contentText="Index.vue"
      :IDList="IDList"
      :buttonBackground="blue"
      @button="closeModal"
      sure="确定"
      alert="1"
    >
    </Modal>
    <button @click="showModel">支付成功模态框</button>

  </div>
</template>

如图

详解如何用VUE写一个多用模态框组件模版

模态框-2.gif

可能并不是特别完美,如果您发现有缺点,还请不吝赐教!

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

Javascript 相关文章推荐
Js 时间间隔计算的函数(间隔天数)
Nov 15 Javascript
如何正确使用javascript 来进行我们的程序开发
Jun 23 Javascript
Jquery实现鼠标移动放大图片功能实例
Mar 25 Javascript
通过正则表达式获取url中参数的简单实现
Jun 07 Javascript
原生 JS Ajax,GET和POST 请求实例代码
Jun 08 Javascript
JS前端加密算法示例
Dec 22 Javascript
详解如何在Angular优雅编写HTTP请求
Dec 05 Javascript
Vue-CLI 3.X 部署项目至生产服务器的方法
Mar 22 Javascript
VUE脚手架的下载和配置步骤详解
Apr 01 Javascript
Layui数据表格 前后端json数据接收的方法
Sep 19 Javascript
关于在LayUI中使用AJAX提交巨坑记录
Oct 25 Javascript
vue-router中hash模式与history模式的区别
Jun 23 Vue.js
解决vue项目nginx部署到非根目录下刷新空白的问题
Sep 27 #Javascript
vue上传图片到oss的方法示例(图片带有删除功能)
Sep 27 #Javascript
浅谈vue项目4rs vue-router上线后history模式遇到的坑
Sep 27 #Javascript
详解关于vue2.0工程发布上线操作步骤
Sep 27 #Javascript
解决betterScroll在vue中存在图片时,出现拉不动的问题
Sep 27 #Javascript
Vue中的v-for指令不起效果的解决方法
Sep 27 #Javascript
在vue中使用v-bind:class的选项卡方法
Sep 27 #Javascript
You might like
[原创]效率较高的php下读取文本文件的代码
2008/07/02 PHP
表格展示无限级分类(PHP版)
2012/08/21 PHP
5种PHP创建数组的实例代码分享
2014/01/17 PHP
PHP图片等比例缩放生成缩略图函数分享
2014/06/10 PHP
JavaScript 题型问答有答案参考
2010/02/17 Javascript
用jQuery打造TabPanel效果代码
2010/05/22 Javascript
javascript代码编写需要注意的7个小细节小结
2011/09/21 Javascript
解析Javascript中难以理解的11个问题
2013/12/09 Javascript
js判断游览器类型及版本号的代码
2014/05/11 Javascript
JavaScript实现获得所有兄弟节点的方法
2015/07/23 Javascript
jquery实现点击其他区域时隐藏下拉div和遮罩层的方法
2015/12/23 Javascript
如何使用vuejs实现更好的Form validation?
2017/04/07 Javascript
深入浅析javascript继承体系
2017/10/23 Javascript
详解vue-cli项目中的proxyTable跨域问题小结
2018/02/09 Javascript
[54:19]完美世界DOTA2联赛PWL S2 Magma vs PXG 第二场 11.28
2020/12/01 DOTA
Python下使用Psyco模块优化运行速度
2015/04/05 Python
在Python的Django框架中实现Hacker News的一些功能
2015/04/17 Python
浅谈python新手中常见的疑惑及解答
2016/06/14 Python
python正则分析nginx的访问日志
2017/01/17 Python
Python绘制二维曲线的日常应用详解
2019/12/04 Python
使用python动态生成波形曲线的实现
2019/12/04 Python
python网络编程socket实现服务端、客户端操作详解
2020/03/24 Python
使用matplotlib动态刷新指定曲线实例
2020/04/23 Python
解决redis与Python交互取出来的是bytes类型的问题
2020/07/16 Python
英语专业毕业生求职简历的自我评价
2013/10/24 职场文书
村级换届选举方案
2014/05/10 职场文书
助人为乐好少年事迹材料
2014/08/18 职场文书
节能环保家庭事迹材料
2014/08/27 职场文书
乡镇党的群众路线教育实践活动总结报告
2014/10/30 职场文书
教师工作表现评语
2014/12/31 职场文书
村党组织公开承诺书
2015/04/30 职场文书
欢送会主持词
2015/07/01 职场文书
2016道德模范先进事迹材料
2016/02/26 职场文书
python 算法题——快乐数的多种解法
2021/05/27 Python
无线电通信名词解释
2022/02/18 无线电
详解Nginx的超时keeplive_timeout配置步骤
2022/05/25 Servers