首页 > javascript相关 > vue教程 > 正文

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

2018-10-22 21:15:41

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

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

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>

效果如图

模态框-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>

如图

模态框-2.gif

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

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

  • 相关标签:vue教程
  • 本文发布HTML5中文学习网 ,转载请注明出处,感谢您!
  • 相关文章


  • 曝网友假装外国人写投诉信 ofo秒退押金并回函致歉
  • 苹果市值缩水逾2000亿美元 遭多家投行下调目标价
  • Asp.net Core与类库读取配置文件信息的方法_实用技巧
  • asp.net在Repeater嵌套的Repeater中使用复选框详解_实用技巧
  • 利用IIS调试ASP.NET网站程序的完整步骤_实用技巧
  • Asp.Net Core轻松学习系列之配置文件_实用技巧
  • ASP.NET 页生命周期概述(小结)_实用技巧
  • 详解ASP.NET Core WebApi 返回统一格式参数_实用技巧
  • 2018年网络流行语有哪些?2018年十大网络流行语盘点
  • 华为首席财务官孟晚舟被暂扣 深圳市政府要求加方立即放人!
  • 独孤九贱(4)_PHP视频教程

    江湖传言:PHP是世界上最好的编程语言。真的是这样吗?这个梗究竟是从哪来的?学会本课程,你就会明白了。 PHP中文网出品的PHP入门系统教学视频,完全从初学者的角度出发,绝不玩虚的,一切以实用、有用...

    独孤九贱(5)_ThinkPHP5视频教程

    ThinkPHP是国内最流行的中文PHP开发框架,也是您Web项目的最佳选择。《php.cn独孤九贱(5)-ThinkPHP5视频教程》课程以ThinkPHP5最新版本为例,从最基本的框架常识开始,将...

    独孤九贱(1)_HTML5视频教程

    《php.cn原创html5视频教程》课程特色:php中文网原创幽默段子系列课程,以恶搞,段子为主题风格的php视频教程!轻松的教学风格,简短的教学模式,让同学们在不知不觉中,学会了HTML知识。 ...

    ThinkPHP5实战之[教学管理系统]

    本套教程,以一个真实的学校教学管理系统为案例,手把手教会您如何在一张白纸上,从零开始,一步一步的用ThinkPHP5框架快速开发出一个商业项目。

    PHP入门视频教程之一周学会PHP

    所有计算机语言的学习都要从基础开始,《PHP入门视频教程之一周学会PHP》不仅是PHP的基础部分更主要的是PHP语言的核心技术,是学习PHP必须掌握的内容,任何PHP项目的实现都离不开这部分的内容,通...

    作者信息

    kevin

    永远在学习的路上!

    相关教程

  • javascript初级视频教程 javascript初级视频教程
  • jquery 基础视频教程 jquery 基础视频教程
  • javascript三级联动视频教程 javascript三级联动视频教程
  • 独孤九贱(3)_JavaScript视频教程 独孤九贱(3)_JavaScript视频教程
  • 独孤九贱(6)_jQuery视频教程 独孤九贱(6)_jQuery视频教程
  • 热门教程