当前位置:首页 > VUE

vue datepicker 实现

2026-01-12 09:53:14VUE

Vue Datepicker 实现方法

使用第三方库(推荐)

安装 vue-datepickerv-calendar 等流行库,例如:

npm install vuejs-datepicker

在组件中引入并使用:

vue datepicker 实现

<template>
  <datepicker v-model="selectedDate"></datepicker>
</template>

<script>
import Datepicker from 'vuejs-datepicker';

export default {
  components: { Datepicker },
  data() {
    return {
      selectedDate: new Date()
    }
  }
}
</script>

自定义实现基础 Datepicker

创建可重用的 Datepicker 组件:

<template>
  <div class="datepicker">
    <input 
      type="text" 
      v-model="displayDate"
      @focus="showPicker = true"
      @blur="handleBlur"
    >
    <div class="calendar" v-if="showPicker">
      <!-- 日历头部 -->
      <div class="header">
        <button @click="prevMonth">←</button>
        <span>{{ currentMonth }} {{ currentYear }}</span>
        <button @click="nextMonth">→</button>
      </div>

      <!-- 日历内容 -->
      <div class="days">
        <div v-for="day in days" :key="day" class="day">
          {{ day }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedDate: null,
      displayDate: '',
      showPicker: false,
      currentDate: new Date(),
      days: ['日', '一', '二', '三', '四', '五', '六']
    }
  },
  computed: {
    currentMonth() {
      return this.currentDate.getMonth() + 1
    },
    currentYear() {
      return this.currentDate.getFullYear()
    }
  },
  methods: {
    prevMonth() {
      this.currentDate = new Date(
        this.currentDate.getFullYear(),
        this.currentDate.getMonth() - 1,
        1
      )
    },
    nextMonth() {
      this.currentDate = new Date(
        this.currentDate.getFullYear(),
        this.currentDate.getMonth() + 1,
        1
      )
    },
    handleBlur() {
      setTimeout(() => {
        this.showPicker = false
      }, 200)
    }
  }
}
</script>

<style>
.datepicker {
  position: relative;
}
.calendar {
  position: absolute;
  border: 1px solid #ccc;
  padding: 10px;
  background: white;
  z-index: 100;
}
</style>

高级功能扩展

为自定义 Datepicker 添加更多功能:

vue datepicker 实现

// 在 methods 中添加日期选择方法
selectDate(date) {
  this.selectedDate = date
  this.displayDate = `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`
  this.showPicker = false
}

// 计算当月天数
getDaysInMonth() {
  return new Date(
    this.currentDate.getFullYear(),
    this.currentDate.getMonth() + 1,
    0
  ).getDate()
}

// 生成日期单元格
generateDates() {
  const daysInMonth = this.getDaysInMonth()
  const dates = []
  for (let i = 1; i <= daysInMonth; i++) {
    dates.push(new Date(
      this.currentDate.getFullYear(),
      this.currentDate.getMonth(),
      i
    ))
  }
  return dates
}

国际化支持

添加多语言支持:

data() {
  return {
    locales: {
      en: {
        days: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
        months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
      },
      zh: {
        days: ['日', '一', '二', '三', '四', '五', '六'],
        months: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']
      }
    },
    currentLocale: 'zh'
  }
}

日期范围限制

添加 minDate 和 maxDate 限制:

props: {
  minDate: {
    type: Date,
    default: null
  },
  maxDate: {
    type: Date,
    default: null
  }
},
methods: {
  isDateDisabled(date) {
    if (this.minDate && date < this.minDate) return true
    if (this.maxDate && date > this.maxDate) return true
    return false
  }
}

标签: vuedatepicker
分享给朋友:

相关文章

简易实现vue

简易实现vue

简易 Vue 实现思路 核心是数据劫持结合发布-订阅模式,通过 Object.defineProperty 或 Proxy 监听数据变化,触发视图更新。 数据劫持实现 class Observer…

vue实现年历

vue实现年历

Vue 实现年历的方法 使用 Vue 实现年历可以通过组件化开发方式,结合日期计算和动态渲染完成。以下是两种常见的实现方式: 基于原生 Vue 的日历组件 创建 Calendar.vue 组件文件,…

vue卖座网实现

vue卖座网实现

Vue 卖座网实现 项目结构搭建 使用 Vue CLI 快速初始化项目,安装必要依赖如 Vue Router、Vuex、Axios。 创建核心目录结构:components(通用组件)、views(页…

vue表格重置怎么实现

vue表格重置怎么实现

重置表格数据的方法 在Vue中重置表格数据通常需要操作绑定的数据源,以下是几种常见实现方式: 直接重置数据源 // 假设表格绑定的是tableData this.tableData = [...t…

vue设计与实现 书

vue设计与实现 书

vue设计与实现 书 《Vue.js设计与实现》是一本深入探讨Vue.js框架内部原理和设计思想的书籍,适合希望深入理解Vue.js的开发者。以下是关于这本书的详细信息: 书籍内容 《Vue.js…

vue调用接口实现退出

vue调用接口实现退出

实现Vue退出功能的步骤 在Vue中调用接口实现退出功能通常需要与后端API交互,以下是实现方法: 前端实现 创建退出方法,调用后端API接口: methods: { logout()…