Vue3 之 computed 属性,实现日历翻月份功能

1. Vue3中的computed函数

1.1. 什么是computed?
computed属性是Vue3中的一个响应式计算属性,它可以根据其他响应式数据的变化而自动更新其自身的值。computed属性可以接收一个计算函数,并在计算函数中使用其他响应式数据的值进行计算。当任何一个参与计算的响应式数据发生变化时,computed属性会自动重新计算其值,并触发相应的依赖更新。
1.2. 如何定义computed?
在Vue3中,你可以通过computed函数来定义一个计算属性。computed函数接收一个计算函数作为参数,并返回一个响应式的ref对象。

下面是一个使用computed函数形式用法的例子:实现日历翻月份功能
 

<template>
  <div>
    <button @click="prevMonth">上一月</button>
    <span>{{ currentMonth }}</span>
    <button @click="nextMonth">下一月</button>
    <ul>
      <li v-for="day in days" :key="day">{{ day }}</li>
    </ul>
  </div>
</template>
 
<script>
import { ref, computed } from 'vue';
 
export default {
  setup() {
    const currentDate = ref(new Date());
    const daysInMonth = computed(() => {
      const date = new Date(currentDate.value.getFullYear(), currentDate.value.getMonth(), 1);
      const days = new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
      return Array.from({ length: days }, (_, i) => i + 1);
    });
 
    const currentMonth = computed(() => {
      const date = currentDate.value;
      return `${date.getFullYear()}年${date.getMonth() + 1}月`;
    });
 
    function prevMonth() {
      currentDate.value = new Date(
        currentDate.value.getFullYear(),
        currentDate.value.getMonth() - 1,
      );
    }
 
    function nextMonth() {
      currentDate.value = new Date(
        currentDate.value.getFullYear(),
        currentDate.value.getMonth() + 1,
      );
    }
 
    return {
      currentMonth,
      daysInMonth,
      prevMonth,
      nextMonth,
    };
  },
};
</script>

这个例子包含了基本的日历展示,以及上一月和下一月的按钮用于翻页。setup函数中定义了响应式数据currentDate和计算属性currentMonthdaysInMonth,分别表示当前月份和天数列表。prevMonthnextMonth函数用于更新currentDate实现翻页功能。

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Vue3 日历组件的实现示例: ```html <template> <div> <div class="calendar-header"> <button @click="prevMonth">上个月</button> <h3>{{ monthStr }}</h3> <button @click="nextMonth">下个月</button> </div> <div class="calendar-grid"> <div v-for="(day, index) in days" :key="index" :class="{ 'calendar-cell': true, 'calendar-cell-disabled': !day }"> {{ day }} </div> </div> </div> </template> <script> import { computed, reactive } from 'vue' export default { setup() { const state = reactive({ currentMonth: new Date(), days: [] }) const prevMonth = () => { state.currentMonth.setMonth(state.currentMonth.getMonth() - 1) } const nextMonth = () => { state.currentMonth.setMonth(state.currentMonth.getMonth() + 1) } const monthStr = computed(() => { return state.currentMonth.toLocaleString('default', { month: 'long', year: 'numeric' }) }) const generateCalendar = () => { const firstDay = new Date(state.currentMonth.getFullYear(), state.currentMonth.getMonth(), 1).getDay() const lastDay = new Date(state.currentMonth.getFullYear(), state.currentMonth.getMonth() + 1, 0).getDate() const days = new Array(35).fill(null) for (let i = 1; i <= lastDay; i++) { days[firstDay + i - 1] = i } state.days = days } generateCalendar() return { state, prevMonth, nextMonth, monthStr } } } </script> <style> .calendar-grid { display: grid; grid-template-columns: repeat(7, 1fr); } .calendar-cell { border: 1px solid #ccc; padding: 10px; text-align: center; } .calendar-cell-disabled { opacity: 0.3; } </style> ``` 这个组件使用了 Vue3 的 `reactive` 和 `computed` 函数来声明响应式状态和计算属性。它还使用了 JavaScript 的 `Date` 对象来计算日历表格中的日期。在 `setup` 函数中,我们定义了一些帮助函数来生成日历,并将它们绑定到组件的方法中。最后,我们通过使用 CSS Grid 布局来组织日历表格的每个单元格。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值