当前位置:首页 > VUE

实现vue datepicker

2026-01-12 23:43:00VUE

以下是实现 Vue Datepicker 的几种方法和步骤:

使用第三方库(如 vue-datepicker)

安装 vue-datepicker 库:

npm install vue-datepicker --save

在 Vue 组件中引入并使用:

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

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

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

自定义基础 Datepicker 组件

创建自定义 Datepicker 组件:

实现vue datepicker

<template>
  <div class="datepicker">
    <input 
      type="text" 
      v-model="displayDate" 
      @focus="showCalendar = true"
      @blur="handleBlur"
    >
    <div class="calendar" v-if="showCalendar">
      <div class="calendar-header">
        <button @click="prevMonth">←</button>
        <span>{{ currentMonth }} {{ currentYear }}</span>
        <button @click="nextMonth">→</button>
      </div>
      <div class="calendar-grid">
        <div v-for="day in days" :key="day" class="calendar-day">
          {{ day }}
        </div>
        <div 
          v-for="date in visibleDates" 
          :key="date.getTime()"
          class="calendar-date"
          :class="{ 'selected': isSelected(date) }"
          @click="selectDate(date)"
        >
          {{ date.getDate() }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedDate: null,
      displayDate: '',
      showCalendar: false,
      currentDate: new Date(),
      days: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
    };
  },
  computed: {
    currentMonth() {
      return this.currentDate.toLocaleString('default', { month: 'long' });
    },
    currentYear() {
      return this.currentDate.getFullYear();
    },
    visibleDates() {
      const year = this.currentDate.getFullYear();
      const month = this.currentDate.getMonth();
      const firstDay = new Date(year, month, 1);
      const lastDay = new Date(year, month + 1, 0);

      let dates = [];
      for (let i = 1; i <= lastDay.getDate(); i++) {
        dates.push(new Date(year, month, i));
      }

      return dates;
    }
  },
  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
      );
    },
    selectDate(date) {
      this.selectedDate = date;
      this.displayDate = date.toLocaleDateString();
      this.showCalendar = false;
    },
    isSelected(date) {
      return this.selectedDate && 
        date.toDateString() === this.selectedDate.toDateString();
    },
    handleBlur() {
      setTimeout(() => {
        this.showCalendar = false;
      }, 200);
    }
  }
};
</script>

<style>
.datepicker {
  position: relative;
  display: inline-block;
}

.calendar {
  position: absolute;
  border: 1px solid #ddd;
  padding: 10px;
  background: white;
  z-index: 1000;
}

.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 5px;
}

.calendar-day, .calendar-date {
  text-align: center;
  padding: 5px;
}

.calendar-date:hover {
  background-color: #eee;
  cursor: pointer;
}

.selected {
  background-color: #4285f4;
  color: white;
}
</style>

使用 Vuetify 的日期选择器

安装 Vuetify:

vue add vuetify

使用 Vuetify 的日期选择器组件:

实现vue datepicker

<template>
  <v-app>
    <v-container>
      <v-date-picker v-model="date"></v-date-picker>
    </v-container>
  </v-app>
</template>

<script>
export default {
  data: () => ({
    date: new Date().toISOString().substr(0, 10)
  })
};
</script>

使用 Element UI 的日期选择器

安装 Element UI:

npm install element-ui -S

在 main.js 中引入:

import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.use(ElementUI);

在组件中使用:

<template>
  <div>
    <el-date-picker v-model="date" type="date"></el-date-picker>
  </div>
</template>

<script>
export default {
  data() {
    return {
      date: ''
    };
  }
};
</script>

每种方法都有其适用场景:第三方库适合快速实现,自定义组件适合特定需求,UI 框架集成适合已有项目架构。根据项目需求选择最合适的方法。

标签: vuedatepicker
分享给朋友:

相关文章

vue实现裁剪头像

vue实现裁剪头像

Vue 实现头像裁剪功能 实现头像裁剪功能通常需要结合第三方库如 cropperjs 或 vue-cropper。以下是两种常见实现方式: 使用 vue-cropper 库 安装依赖: npm i…

vue实现右下角弹框

vue实现右下角弹框

实现右下角弹框的基本思路 在Vue中实现右下角弹框,通常需要结合CSS定位和Vue的组件化特性。弹框可以通过绝对定位固定在右下角,并通过Vue控制其显示与隐藏。 创建弹框组件 新建一个Vue组件(如…

vue实现sso

vue实现sso

Vue 实现 SSO(单点登录)方案 单点登录(SSO)允许用户通过一次登录访问多个相关系统。以下是基于 Vue 的实现方案: 使用 OAuth2/OpenID Connect 协议 OAuth2…

vue评分实现

vue评分实现

Vue 评分组件实现方法 使用第三方库(如 Element UI) 安装 Element UI: npm install element-ui 引入并注册组件: import Vue from '…

vue compile 实现

vue compile 实现

Vue 编译实现原理 Vue 的编译过程将模板字符串转换为渲染函数,主要分为解析、优化和代码生成三个阶段。 解析阶段(Parse) 将模板字符串转换为抽象语法树(AST)。Vue 使用正则表达式和有…

vue实现tap

vue实现tap

Vue 实现 Tap 事件 在移动端开发中,Tap 事件比 Click 事件更常用,因为 Click 事件有 300ms 延迟。以下是几种在 Vue 中实现 Tap 事件的方法。 使用第三方库 安装…