当前位置: 首页 > news >正文

vue3前一月/年+后一月/年

在这里插入图片描述
在这里插入图片描述

// 按钮文字
export const BUTTON_CONFIG = {
  1: ["前一月", "后一月"],
  2: ["前一年", "后一年"],
};
<template>
  <div class="change-date flex-row">
    <div
      class="flex-center"
      v-for="(item, index) in BUTTON_CONFIG[type]"
      :key="item"
      @click="handleDate(index)"
    >
      {{ item }}
    </div>
  </div>
</template>
<script setup name="changeDate">
import dayjs from "dayjs";
import { BUTTON_CONFIG } from "../consts/detail";
const props = defineProps({
  // 类型 1-前一月,后一月 2-前一年,后一年
  type: {
    type: String,
    default: "1",
  },
  // 日期
  date: {
    type: String,
    default: "",
  },
});
const emits = defineEmits(["update:date", "update"]);
// 切换日期
function handleDate(index) {
  const { type, date } = props;

  // 确定时间单位("month""year")
  const unit = type === "1" ? "month" : "year";

  // 计算增减的偏移量
  const offset = index ? 1 : -1;

  // 更新日期并格式化
  const formattedDate = dayjs(date)
    .add(offset, unit)
    .format(type === "1" ? "YYYY-MM" : "YYYY");

  emits("update:date", formattedDate);
  emits("update");
}
</script>
<style lang="less" scoped>
.change-date {
  width: 140px;
  height: 36px;
  margin-right: 20px;
  background: #f1f1f1;
  border-radius: 4px;
  border: 1px solid #d5d5d5;
  font-weight: 400;
  font-size: 14px;
  color: #6d6d6d;
  line-height: 21px;
  div {
    width: 50%;
    height: 100%;
    cursor: pointer;
    &:first-child {
      border-right: 1px solid #d5d5d5;
    }
    &:hover {
      color: #1f63ff;
    }
  }
}
</style>

相关文章:

  • 基于springcloud的“微服务架构的巡游出租管理平台”的设计与实现(源码+数据库+文档+PPT)
  • 【Linux】jumpserver开源堡垒机部署
  • 【时时三省】(C语言基础)选择结构的嵌套
  • Linux 时间同步工具 Chrony 简介与使用
  • Java学习——day25(多线程基础与线程创建方式)
  • idea 安装 proxyai 后的使用方法
  • DAPP实战篇:使用web3.js连接合约
  • java设计模式-模板方法模式
  • 【JavaSE】异常
  • OSPF基础入门篇②:OSPF邻居建立篇-网络设备的“社交礼仪“
  • Android里蓝牙使用流程以及问题详解
  • 深度强化学习基础 0:通用学习方法
  • 架构演进成熟度校验体系构建
  • MyBatis批量更新之CASE WHEN方式详解
  • 找搭子系统 搭子经济新风口 基于精准匹配的社交新生态探索
  • React + TipTap 富文本编辑器 实现消息列表展示,类似Slack,Deepseek等对话框功能
  • 基于二叉堆实现的 PriorityQueue
  • LLM应用实战2-理解Tokens
  • C语言malloc类函数详解
  • Linux C 与 C 语言的区别及开发差异
  • 网站建设成果/西安seo外包行者seo06
  • 房地产交易网站模版/上海推广网站
  • 河南省百城建设提质工程官网网站/企业网站建设报价
  • 织梦html网站地图/百度点击率排名有效果吗
  • 庆阳市人大常委会网站建设/seo能干一辈子吗
  • 做jsp网站用哪些软件下载/磁力天堂最新版地址