您好,登錄后才能下訂單哦!
這篇文章主要介紹了基于Vue3怎么實現日歷組件的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇基于Vue3怎么實現日歷組件文章都會有所收獲,下面我們一起來看看吧。
以下是一個基于 Vue 3 實現的簡單日歷組件的代碼示例。這個日歷組件包含了前一個月、當前月、下一個月的日期,并且可以支持選擇日期、切換月份等功能。
<template> <div class="calendar"> <div class="header"> <button class="prev" @click="prevMonth"><</button> <div class="title">{{ title }}</div> <button class="next" @click="nextMonth">></button> </div> <div class="weekdays"> <div v-for="day in daysOfWeek" :key="day" class="day">{{ day }}</div> </div> <div class="days"> <div v-for="day in days" :key="day.date" :class="{ today: isToday(day), selected: isSelected(day), notCurrentMonth: isNotCurrentMonth(day), }" @click="select(day)" > {{ day.day }} </div> </div> </div> </template> <script> import { ref, computed } from "vue"; export default { name: "FeiCalendar", props: { selectedDate: Date, }, emits: ["update:selectedDate"], setup(props, { emit }) { const weekdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; const currentDate = ref(new Date()); const selectedDate = ref(props.selectedDate || currentDate.value); const daysOfWeek = computed(() => { return weekdays; }); const days = computed(() => { const year = currentDate.value.getFullYear(); const month = currentDate.value.getMonth(); const daysInMonth = new Date(year, month + 1, 0).getDate(); const daysInLastMonth = new Date(year, month, 0).getDate(); const firstDayOfMonth = new Date(year, month, 1).getDay(); const days = []; let day = 1; let lastMonthDay = daysInLastMonth - firstDayOfMonth + 1; let nextMonthDay = 1; for (let i = 0; i < 6 * 7; i++) { if (i < firstDayOfMonth) { days.push({ date: new Date(year, month - 1, lastMonthDay), day: lastMonthDay, isLastMonth: true, isNextMonth: false, }); lastMonthDay++; } else if (i >= firstDayOfMonth + daysInMonth) { days.push({ date: new Date(year, month + 1, nextMonthDay), day: nextMonthDay, isLastMonth: false, isNextMonth: true, }); nextMonthDay++; } else { const date = new Date(year, month, day); days.push({ date, day, isLastMonth: false, isNextMonth: false }); day++; } } return days; }); const title = computed( () => `${currentDate.value.toLocaleString("default", { month: "long", })} ${currentDate.value.getFullYear()}` ); const prevMonth = () => { currentDate.value = new Date( currentDate.value.getFullYear(), currentDate.value.getMonth() - 1, 1 ); }; const nextMonth = () => { currentDate.value = new Date( currentDate.value.getFullYear(), currentDate.value.getMonth() + 1, 1 ); }; const isToday = (day) => { const today = new Date(); return day.date.toDateString() === today.toDateString(); }; const isSelected = (day) => { return day.date.toDateString() === selectedDate.value.toDateString(); }; const isNotCurrentMonth = (day) => { return day.isLastMonth || day.isNextMonth; }; const select = (day) => { selectedDate.value = day.date; emit("update:selectedDate", day.date); }; return { daysOfWeek, days, title, prevMonth, nextMonth, isToday, isSelected, isNotCurrentMonth, select, }; }, }; </script> <style> .calendar { max-width: 500px; margin: 0 auto; font-family: Arial, sans-serif; } .header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; } .title { font-size: 18px; font-weight: bold; } .weekdays { display: flex; justify-content: space-around; margin-bottom: 10px; } .day { width: 30px; height: 30px; display: flex; justify-content: center; align-items: center; border-radius: 50%; } .days { display: grid; grid-template-columns: repeat(7, 1fr); grid-gap: 10px; } .today { background-color: lightblue; } .selected { background-color: blue; color: white; } .notCurrentMonth { color: #ccc; } </style>
使用該組件時,可以將selectedDate
屬性綁定到一個父組件中的數據,這個數據將會存儲選中的日期。例如:
<template> <div> <!-- 用法一 --> <FeiCalendar :selectedDate="selectedDate" @update:selectedDate="onSelectedDateUpdated" /> <!-- 用法二 --> <!-- <FeiCalendar v-model:selectedDate="selectedDate" /> --> <p>Selected date: {{ selectedDate }}</p> </div> </template> <script> import FeiCalendar from "./FeiCalendar.vue"; export default { components: { FeiCalendar, }, data() { return { selectedDate: new Date(), }; }, watch: { selectedDate(nv) { console.log("nv", nv); }, }, methods: { onSelectedDateUpdated(selectedDate) { this.selectedDate = selectedDate; }, }, }; </script>
這是一個簡單的示例,可以根據自己的需求對代碼進行修改和擴展。
關于“基于Vue3怎么實現日歷組件”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“基于Vue3怎么實現日歷組件”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。