广告
返回顶部
首页 > 资讯 > 精选 >如何封装vue日历组件
  • 807
分享到

如何封装vue日历组件

2023-06-29 10:06:12 807人浏览 安东尼
摘要

这篇文章将为大家详细讲解有关如何封装Vue日历组件,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。具体内容如下图示封装的组件的代码如下<template>  <div&

这篇文章将为大家详细讲解有关如何封装Vue日历组件,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

具体内容如下

图示

如何封装vue日历组件

封装的组件的代码如下

<template>  <div class="calendar">    <!-- 选择日历的弹出层 -->    <div class="model_mask" v-show="showtimemask" @click="showmask1()">    </div>    <div class="bouncedBox" v-show="showtimemask">      <div class="mobile-top">        <div class="sel-time">          <p>开始时间</p>          <p class="start-date">{{starttime.substring(0,4)+'-'+starttime.substring(4,6)+'-'+starttime.substring(6,8)}}          </p>        </div>        <div class="unsel-time">          <p>结束时间</p>          <p class="end-date">            {{endtime==''?'请选择结束日期':endtime.substring(0,4)+'-'+endtime.substring(4,6)+'-'+endtime.substring(6,8)}}</p>        </div>      </div>       <div class="title">        <div class="btn" @click.stop="last()" :class="(month<=nowmonth)&&(Year<=nowYear)?'noclick':'' ">上一月</div>        <div class="text">{{Year}}年{{month}}月</div>        <div class="btn" @click.stop="next()">下一月</div>      </div>       <div class="head">        <div class="days" v-for="(item,index) in ['星期日','星期一','星期二','星期三','星期四','星期五','星期六']" :key="index">          {{item}}        </div>      </div>       <div class="wrap">        <div class="span" v-for="(item,index) in calendarList" :key="index" @click.stop="click(item.count)" :class="item==''?'kong'      :item.count<nowtime?'noclick'      :(item.count>=starttime&&item.count<=endtime)||item.count==starttime?'active':''">          {{item.value}}        </div>      </div>       <div class="bottombtn">        <button class="cancle-btn" @click.stop='cancle()'>取消</button>        <button class="sure-btn" @click.stop='firm()'>确定</button>      </div>     </div>  </div></template> <script>  export default {    name: "Calendar",    data() {      return {        showtimemask:false,        Puton_time: '', //投放日期  默认今日 展示        Puton_Start:'',  //为了保存投放开始结束的日期  用来点击取消按钮时初始化选中的值        Puton_End:'',        nowtime: '', //当前日期的时间-----20190203格式  用于比较        clickitem: '', //保存每次点击的时间-----20190203格式  用于比较        clickcount: 0, //点击次数-------判断开始时间还是结束时间        starttime: '', //开始时间  数字   默认当天日期        endtime: '', //结束时间  数字   默认当天日期        Year: new Date().getFullYear(), //日历上的年份   ----动态改变的        month: new Date().getMonth() + 1, //日历上的月份 ----  动态改变的        Day: new Date().getDate(), //日历上的天份         ----- 动态改变的         nowYear: new Date().getFullYear(),        nowmonth: new Date().getMonth() + 1,        nowDay: new Date().getDate(),        calendarList: [],      };    },     created() {      //关于日历的操作开始      this.Draw(this.nowYear, this.nowmonth);       let time_month = this.nowmonth; //现在的月份      let time_day = this.nowDay; //现在的天数      if (this.nowmonth < 10) {        time_month = 0 + '' + this.nowmonth;      }      if (this.nowDay < 10) {        time_day = 0 + '' + this.nowDay;      }       this.nowtime = this.nowYear + '' + time_month + '' + time_day;      this.starttime = this.nowtime;      this.endtime = this.nowtime;       this.Puton_time = this.starttime.substring(0, 4) + '-' + this.starttime.substring(4, 6) + '-' + this.starttime        .substring(6, 8) + '至今';         this.Puton_Start = this.nowtime,        this.Puton_End = this.nowtime,          this.$emit('str',this.Puton_time)       //关于日历的操作结束    },    mounted() {     },    methods: {      showmask1() {        if (this.showtimemask == true) {          // this.showtimemask=false;   //隐藏弹框          this.cancle();        } else {          this.showtimemask = true;  //显示弹框        }      },       Draw: function (Year, Month) {        //日期列表        var calendar = [];         //用当月第一天在一周中的日期值作为当月离第一天的天数(获取当月第一天是周几)        for (var i = 1, firstDay = new Date(Year, Month - 1, 1).getDay(); i <= firstDay; i++) {          calendar.push("");        }         //用当月最后一天在一个月中的日期值作为当月的天数        for (var i = 1, monthDay = new Date(Year, Month, 0).getDate(); i <= monthDay; i++) {           let time_month = Month;          let time_day = i;          if (Month < 10) {            time_month = 0 + '' + Month;          }          if (i < 10) {            time_day = 0 + '' + i;          }           calendar.push({            value: i,            count: Year + '' + time_month + '' + time_day          })        }        this.calendarList = calendar;        console.log(calendar)      },       last() {        this.month--;        if (this.month == 0) {          this.month = 12;          this.Year--;        }         this.Draw(this.Year, this.month);      },       next() {        this.month++;        if (this.month == 13) {          this.month = 1;          this.Year++;        }         this.Draw(this.Year, this.month);      },       click(item) {        this.clickcount++;        this.clickitem = item;        //开始日期        if (this.clickcount % 2 == 1) {          this.starttime = this.clickitem;          this.endtime = ''        } else {          this.endtime = this.clickitem;          if (this.starttime > this.endtime) {            this.endtime = this.starttime;            this.starttime = this.clickitem;          }        }      },       firm() {        this.showtimemask = false;        //当选择的开始时间与结束时间相同时   显示为2019-07-19当天        if (this.starttime == this.endtime) {          this.Puton_Start = this.starttime,          this.Puton_End = this.endtime,           this.Puton_time = this.starttime.substring(0, 4) + '-' + this.starttime.substring(4, 6) + '-' + this.starttime            .substring(6, 8) + '当天';             this.$emit('str',this.Puton_time);             //否则显示xxx 至   xxx        } else {           this.Puton_Start = this.starttime,          this.Puton_End = this.endtime,          this.Puton_time =            this.starttime.substring(0, 4) + '-' + this.starttime.substring(4, 6) + '-' + this.starttime.substring(6,            8) +            '至' + this.endtime.substring(0, 4) + '-' + this.endtime.substring(4, 6) + '-' + this.endtime.substring(6, 8);                           this.$emit('str',this.Puton_time)        }       },      // 取消按钮      cancle() {        this.showtimemask = false;         //当按取消按钮时   弹框中选中的区域等于上一次选中的区域        this.starttime = this.Puton_Start;        this.endtime = this.Puton_End;        // this.Puton_time = this.starttime.substring(0, 4) + '-' + this.starttime.substring(4, 6) + '-' + this.starttime        //   .substring(6, 8) + '至今';         //   this.$emit('str',this.Puton_time)      }    }  }; </script> <style scoped lang="sCSS">  @import "../common/common.css";   // 日历的样式  .model_mask {    position: fixed;    top: 0;    bottom: 0;    left: 0;    right: 0;    background: rgba($color: #000000, $alpha: 0.5);  }   .bouncedBox {    position: fixed;    background: #fff;    bottom: 0;    left: 0;    right: 0;     //开始结束日期的显示    .mobile-top {      display: flex;      flex-wrap: nowrap;      background: #fff;      padding: 0.1rem 0;       .sel-time {        text-align: center;        width: 50%;         // border-bottom: solid 2px #2a81e8;        .start-date {          color: #b1b1b1;          margin-top: 0.05rem;        }      }       .unsel-time {        text-align: center;        width: 50%;         .end-date {          color: #b1b1b1;          margin-top: 0.05rem;        }      }    }     // 左右选择月份  显示当前年月    .title {      width: 100%;      height: 40px;      background-color: #60a7e8;      display: flex;      flex-wrap: nowrap;      text-align: center;      color: #fff;      font-weight: bold;      line-height: 40px;       .btn {        width: 1.2rem;         &.noclick {          pointer-events: none;          background: #ccc;        }      }       .text {        flex: 1;      }    }     //表头  周1到周天的显示    .head {      display: flex;      flex-wrap: nowrap;      text-align: center;      height: 40px;      line-height: 40px;       .days {        flex: 1;      }    }     //日历表区域    .wrap {      width: 7.5rem;      height: auto;      overflow: hidden;      padding-bottom: 1rem;       .span {        width: 1.07142rem;        height: 0.6rem;        background: #fff;        color: #337ab7;        float: left;        text-align: center;        line-height: 0.6rem;         &.active {          background: #037ef5;          color: #fff;        }         &.noclick {          pointer-events: none;          background: #ccc;        }         &.kong {          background: #fff;          pointer-events: none;        }      }    }     //底部按钮区域    .bottombtn {      height: 40px;      width: 100%;      display: flex;      flex-wrap: nowrap;       button {        flex: 1;      }       .sure-btn {        background: #037ef5;         color: #fff;      }    }   } </style>

使用方法

main,js引入  全局注册组件

import Calendar from './components/fz_zujian/Calendar.vue'    //日历组件Vue.component('Calendar',Calendar)

页面使用

<div class="" @click="showmodel()">{{str}}</div> <Calendar ref="chi1" v-on:str="getChild"></Calendar>  data() {      return {        str: '',      }  }  showmodel(){        this.$refs.chi1.showmask1()      },       getChild(val) {        this.str = val      },

关于“如何封装vue日历组件”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

--结束END--

本文标题: 如何封装vue日历组件

本文链接: https://www.lsjlt.com/news/324232.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 如何封装vue日历组件
    这篇文章将为大家详细讲解有关如何封装vue日历组件,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。具体内容如下图示封装的组件的代码如下<template>  <div&...
    99+
    2023-06-29
  • vue日历组件的封装方法
    本文实例为大家分享了vue日历组件的封装代码,供大家参考,具体内容如下 图示 封装的组件的代码如下 <template>   <div class="calend...
    99+
    2022-11-13
  • 深析如何封装一个vue自定义日历组件
    执行这个方法之后,此时calendarProps的值为:4、根据日历属性生成日历日期的数据当我们已经知道本月第一天对应的周几索引值、本月一共有多少天和上个月一共有多少天这三个核心数据之后,就可以开始生成对应的日历数据了。思路如下:由于大部分...
    99+
    2023-05-14
    Vue.js 数据可视化 前端
  • 如何使用vue封装一个自定义日历组件
    本文小编为大家详细介绍“如何使用vue封装一个自定义日历组件”,内容详细,步骤清晰,细节处理妥当,希望这篇“如何使用vue封装一个自定义日历组件”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。核心代码实现1、梳理思...
    99+
    2023-07-05
  • 怎么封装一个vue自定义日历组件
    这篇文章主要介绍“怎么封装一个vue自定义日历组件”,在日常操作中,相信很多人在怎么封装一个vue自定义日历组件问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么封装一个vue自定义日历组件”的疑惑有所帮助!...
    99+
    2023-07-05
  • vue组件如何封装
    封装vue组件的方法:1.新建vue.js项目;2.使用Vue.extend()方法创建组件;3.使用Vue.component()方法注册组件;4.构建组件变量;5.使用组件名称标签调用组件;具体步骤如下:首先,在vue-cli中创建一个...
    99+
    2022-10-06
  • 详解怎么使用vue封装一个自定义日历组件
    执行这个方法之后,此时calendarProps的值为:4、根据日历属性生成日历日期的数据当我们已经知道本月第一天对应的周几索引值、本月一共有多少天和上个月一共有多少天这三个核心数据之后,就可以开始生成对应的日历数据了。思路如下:由于大部分...
    99+
    2023-05-14
    Vue.js 数据可视化 前端
  • 如何实现vue日历组件
    这篇文章主要介绍了如何实现vue日历组件,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。1. 前言最近做项目遇到一个需求,需要制作一个定制化的日历组件(项目使用的UI框架不能满...
    99+
    2023-06-29
  • vue如何封装TabBar组件
    这篇文章主要为大家展示了“vue如何封装TabBar组件”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“vue如何封装TabBar组件”这篇文章吧。实现思路:步骤一:TabBar和TabBarIt...
    99+
    2023-06-25
  • vue实现日历组件
    基于VUE实现日历组件,供大家参考,具体内容如下 年和月份是使用输入框来切换的,没有做成选择框,⬅️和➡️切换月份,红色选取是选取的日期实现思路和网上的大多数一样,首先是把月份的天...
    99+
    2022-11-13
  • Vue分页组件如何封装
    今天小编给大家分享一下Vue分页组件如何封装的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。效果如图话不多说,直接上代码<...
    99+
    2023-07-02
  • 从零写vue日历组件
    目录1. 前言2. vue日历制作2.1 制作月份选择器2.2 制作日历2.2.1 获取当前月所要显示的日期2.2.2 给不同的日期添加不同的样式2.3 将月份选择器和日历组件组合使...
    99+
    2022-11-13
  • Vue如何封装全局toast组件
    本篇内容主要讲解“Vue如何封装全局toast组件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Vue如何封装全局toast组件”吧!一. 借助 vue-cli...
    99+
    2022-10-19
  • 如何使用vue组件封装共用的组件
    这篇文章主要介绍了如何使用vue组件封装共用的组件的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇如何使用vue组件封装共用的组件文章都会有所收获,下面我们一起来看看吧。这里提供两种vue封装共用组件的方法方法一...
    99+
    2023-06-30
  • Vue封装svg-icon组件如何使用
    这篇文章主要介绍“Vue封装svg-icon组件如何使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Vue封装svg-icon组件如何使用”文章能帮助大家解决问题。一、SVG可缩放矢量图形SVG(...
    99+
    2023-07-05
  • vue中如何封装echarts公共组件
    这篇文章主要讲解了“vue中如何封装echarts公共组件”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“vue中如何封装echarts公共组件”吧!定义图表公共样式是为了统一同一网站各页面图...
    99+
    2023-06-30
  • vue如何自定义封装API组件
    目录自定义封装API组件1.创建vue组件2.创建Alter.js生成组件3.导入Vue如何封装使用api形式调用的vue组件子组件父组件自定义封装API组件 1.创建vue组件 &...
    99+
    2022-11-13
  • vue中如何将echart封装为组件
    这篇文章主要介绍“vue中如何将echart封装为组件”,在日常操作中,相信很多人在vue中如何将echart封装为组件问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”vue中如何将echart封装为组件”的疑...
    99+
    2023-07-04
  • bootstrap中select插件如何封装成Vue组件
    本篇内容主要讲解“bootstrap中select插件如何封装成Vue组件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“bootstrap中select插件如何封装成Vue组件”吧!因为boot...
    99+
    2023-07-04
  • Vue中如何对ElementUI的Dialog组件封装
    目录对ElementUI的Dialog组件封装.sync 修饰符说明总结对ElementUI的Dialog组件封装 1.子组件的写法 <el-dialog     title=...
    99+
    2023-03-11
    Vue ElementUI Dialog组件封装 Vue ElementUI封装
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作