vue-calendar-component 封裝多日期選擇組件的實(shí)例代碼
實(shí)現(xiàn)效果


安裝vue-calendar-component日歷組件
cnpm i vue-calendar-component --save //國(guó)內(nèi)鏡像
引入
import Calendar from "vue-calendar-component";
export default {
components: { Calendar },
}
封裝
<template>
<div class="x-f">
<Calendar
ref="Calendar"
v-on:choseDay="clickDay"
:style="{
height: '4.4rem',
width: '4rem',
fontSize: '0.2rem !important',
}"
></Calendar>
<Calendar
v-if="multiple"
ref="Calendar2"
v-on:choseDay="clickDay2"
:style="{
height: '4.4rem',
width: '4rem',
fontSize: '0.2rem !important',
}"
></Calendar>
</div>
</template>
<script>
import { formatDate } from "@/lib/date_fun.js";
import Calendar from "vue-calendar-component";
export default {
components: { Calendar },
props: {
value: {
//v-model雙向綁定
type: String,
default: () => {
return "";
},
},
// 是否多選
multiple: {
type: Boolean,
default: () => {
return true;
},
},
// 兩個(gè)日期之間的間隔符
separator: {
type: String,
default: () => {
return "至";
},
},
},
data() {
return {
tap1: false, //日歷組件1是否點(diǎn)擊選中
tap2: false, //日歷組件2是否點(diǎn)擊選中
rqf: "",
rqt: "",
};
},
created() {
//初始化設(shè)置日期選中
this.$nextTick(() => {
if (this.value.indexOf(this.separator) == -1) {
this.$refs.Calendar.ChoseMonth(this.value);
} else {
this.$refs.Calendar.ChoseMonth(this.value.split(this.separator)[0]);
this.$refs.Calendar2.ChoseMonth(this.value.split(this.separator)[1]);
}
});
},
watch: {
value(date) {
//監(jiān)聽(tīng)整個(gè)日歷組件值,設(shè)置選中狀態(tài)
this.$nextTick(() => {
if (date.indexOf(this.separator) > -1) {
this.$refs.Calendar.ChoseMonth(date.split(this.separator)[0]);
this.$refs.Calendar2.ChoseMonth(date.split(this.separator)[1]);
} else {
this.$refs.Calendar.ChoseMonth(date);
}
});
},
},
methods: {
clickDay(date) {
//日期1點(diǎn)擊事件
this.tap1 = true;
this.rqf = formatDate(date);
this.comit();
},
clickDay2(date) {
//日期2點(diǎn)擊事件
this.tap2 = true;
this.rqt = formatDate(date);
this.comit();
},
comit() {
//判斷是否多選全部點(diǎn)擊選中,或者單選點(diǎn)擊選中
if (this.tap1 && (this.tap2 || !this.multiple)) {
let mrq = "";
if (this.multiple) mrq = this.rqf + this.separator + this.rqt;
//多選賦值格式
else mrq = this.rqf; //單選賦值格式
this.$emit("input", mrq); //給v-model賦值;
//賦值結(jié)束,重設(shè)點(diǎn)擊狀態(tài)標(biāo)識(shí)
this.tap1 = false;
this.tap2 = false;
}
},
},
};
</script>
css樣式
/* 日歷組件 */
.wh_content_all{
background-color: #ffffff !important;
}
.wh_top_changge li{
color: rgb(50, 50, 51) !important;
font-size: 0.18rem !important;
}
.wh_jiantou1{
width: 0.1rem !important;
height: 0.1rem !important;
border-top: 2px solid rgb(50, 50, 51) !important;
border-left: 2px solid rgb(50, 50, 51) !important;
}
.wh_jiantou2{
width: 0.1rem !important;
height: 0.1rem !important;
border-top: 2px solid rgb(50, 50, 51) !important;
border-right: 2px solid rgb(50, 50, 51) !important;
}
.wh_top_tag{
font-size: 0.16rem !important;
}
.wh_item_date{
font-size: 0.2rem !important;
}
.wh_content_item,
.wh_content_item_tag{
width: 14.285% !important;
color: rgb(50, 50, 51) !important;
}
.wh_content_item .wh_isToday{
background: transparent !important;
}
.wh_content_item .wh_chose_day{
background: #ee0a24 !important;
color: #fff !important;
}
.wh_content{
padding: 0 0.1rem !important;
}
.wh_container{
position: relative;
}
.wh_container::after{
content: '';
width: 1px;
height: 100%;
position: absolute;
right: 0;
top: 0;
background: #dddddd;;
}
頁(yè)面調(diào)用
<template>
<div class="form-item mt20 x-f">
<span class="form-label">日期</span>
<van-popover v-model="showPopover" placement="bottom-end">
<mCalendar v-model="rq" :multiple="multiple" :separator="separator"/>
<template #reference>
<span class="form-input" @click="showPopover = !showPopover">{{ rq}}</span>
</template>
</van-popover>
</div>
</template>
<script>
import mCalendar from "@/components/mCalendar.vue";
import { getNowDate } from '@/lib/date_fun.js'
export default {
components: { mCalendar },
data () {
return {
showPopover:false,
rq: getNowDate(),
multiple: false, //日期是否多選
separator: '至' //兩個(gè)日期之間的間隔符
}
},
watch:{
//日期放生變化是隱藏日歷Popover
rq(){
this.showPopover = false;
}
}
}
</script>
寫(xiě)的不完善,有待優(yōu)化
到此這篇關(guān)于vue-calendar-component 封裝多日期選擇組件的文章就介紹到這了,更多相關(guān)vue-calendar-component 日期選擇組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue系列:通過(guò)vue-router如何傳遞參數(shù)示例
本篇文章主要介紹了Vue系列:通過(guò)vue-router如何傳遞參數(shù)示例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01
Vue3?vue-devtools?調(diào)試工具下載安裝使用教程
vue-devtools是一款基于chrome游覽器的插件,用于調(diào)試vue應(yīng)用,這可以極大地提高我們的調(diào)試效率,尤其是對(duì)于初學(xué)vue的朋友來(lái)說(shuō)可謂是一大利器,這篇文章主要介紹了Vue3?vue-devtools?調(diào)試工具下載安裝,需要的朋友可以參考下2022-08-08
vue監(jiān)聽(tīng)滾動(dòng)事件實(shí)現(xiàn)滾動(dòng)監(jiān)聽(tīng)
本文主要介紹了vue監(jiān)聽(tīng)滾動(dòng)事件實(shí)現(xiàn)滾動(dòng)監(jiān)聽(tīng)的相關(guān)資料。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-04-04
利用Vue+intro.js實(shí)現(xiàn)頁(yè)面新手引導(dǎo)流程功能
在同學(xué)們使用某些網(wǎng)站的新版本頁(yè)面的時(shí)候,經(jīng)常會(huì)出現(xiàn)一個(gè)類(lèi)似于新手引導(dǎo)一樣的效果,來(lái)幫助同學(xué)們更好的熟悉新版本頁(yè)面的功能和使用,這篇文章主要給大家介紹了關(guān)于如何利用Vue+intro.js實(shí)現(xiàn)頁(yè)面新手引導(dǎo)流程功能的相關(guān)資料,需要的朋友可以參考下2023-11-11
Vue源碼學(xué)習(xí)記錄之手寫(xiě)vm.$mount方法
在我們開(kāi)發(fā)中,經(jīng)常要用到Vue.extend創(chuàng)建出Vue的子類(lèi)來(lái)構(gòu)造函數(shù),通過(guò)new 得到子類(lèi)的實(shí)例,然后通過(guò)$mount掛載到節(jié)點(diǎn),今天通過(guò)本文給大家講解手寫(xiě)vm.$mount方法 ,感興趣的朋友一起看看吧2022-11-11
VUE3?Vite打包后動(dòng)態(tài)圖片資源不顯示問(wèn)題解決方法
這篇文章主要給大家介紹了關(guān)于VUE3?Vite打包后動(dòng)態(tài)圖片資源不顯示問(wèn)題的解決方法,可能是因?yàn)樵诓渴鸷蟮姆?wù)器環(huán)境中對(duì)中文文件名的支持不完善,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-09-09
vue實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)和參數(shù)傳遞的兩種方式
這篇文章主要介紹了vue頁(yè)面跳轉(zhuǎn)和參數(shù)傳遞的兩種方式,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09
vue ElementUI實(shí)現(xiàn)異步加載樹(shù)
這篇文章主要為大家詳細(xì)介紹了vue ElementUI實(shí)現(xiàn)異步加載樹(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
el-tree樹(shù)狀控件如何定位到選中的節(jié)點(diǎn)的位置
這篇文章主要介紹了el-tree樹(shù)狀控件如何定位到選中的節(jié)點(diǎn)的位置,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-08-08

