当前位置:首页 > VUE

vue实现日期组件

2026-02-20 11:23:44VUE

Vue 日期组件的实现方法

使用原生 HTML5 input[type="date"]

<template>
  <input type="date" v-model="selectedDate" @change="handleDateChange">
</template>

<script>
export default {
  data() {
    return {
      selectedDate: ''
    }
  },
  methods: {
    handleDateChange() {
      console.log('Selected date:', this.selectedDate)
    }
  }
}
</script>

这种方法简单直接,但样式和功能受浏览器限制。

使用第三方库(如 Element UI)

安装 Element UI:

npm install element-ui -S

注册组件后使用:

<template>
  <el-date-picker
    v-model="value"
    type="date"
    placeholder="选择日期"
    @change="handleChange">
  </el-date-picker>
</template>

<script>
export default {
  data() {
    return {
      value: ''
    }
  },
  methods: {
    handleChange(val) {
      console.log('Selected date:', val)
    }
  }
}
</script>

自定义日期选择器组件

创建自定义组件 DatePicker.vue

<template>
  <div class="date-picker">
    <input 
      type="text" 
      v-model="displayDate"
      @focus="showCalendar = true"
      @blur="handleBlur">

    <div class="calendar" v-show="showCalendar">
      <div class="calendar-header">
        <button @click="prevMonth">←</button>
        <span>{{ currentMonth }}</span>
        <button @click="nextMonth">→</button>
      </div>
      <div class="calendar-body">
        <!-- 日期渲染逻辑 -->
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      displayDate: '',
      showCalendar: false,
      currentDate: new Date()
    }
  },
  computed: {
    currentMonth() {
      return this.currentDate.toLocaleString('default', { month: 'long', year: 'numeric' })
    }
  },
  methods: {
    prevMonth() {
      this.currentDate = new Date(this.currentDate.setMonth(this.currentDate.getMonth() - 1))
    },
    nextMonth() {
      this.currentDate = new Date(this.currentDate.setMonth(this.currentDate.getMonth() + 1))
    },
    handleBlur() {
      setTimeout(() => {
        this.showCalendar = false
      }, 200)
    }
  }
}
</script>

使用 Vue 3 Composition API

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

<script>
import { ref } from 'vue'
import DatePicker from 'v-calendar/lib/components/date-picker.umd'

export default {
  components: {
    DatePicker
  },
  setup() {
    const date = ref(new Date())
    return { date }
  }
}
</script>

关键功能实现要点

日期格式化可使用 moment.jsdate-fns

import { format } from 'date-fns'

format(new Date(), 'yyyy-MM-dd')

处理日期范围:

methods: {
  isDisabledDate(date) {
    return date < new Date() // 禁用过去的日期
  }
}

样式优化建议

为自定义组件添加基础样式:

.date-picker {
  position: relative;
}

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

.calendar-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}

vue实现日期组件

标签: 组件日期
分享给朋友:

相关文章

vue怎么实现组件缓存

vue怎么实现组件缓存

vue实现组件缓存的方法 在Vue中实现组件缓存可以通过内置的<keep-alive>组件完成,该组件能够缓存不活动的组件实例,避免重复渲染。 使用<keep-alive>基…

vue实现倒计时组件

vue实现倒计时组件

Vue 倒计时组件实现 核心思路 通过 setInterval 或 setTimeout 实现时间递减,结合 Vue 的响应式数据更新 UI。需注意组件销毁时清除定时器。 基础实现方案 模板部分…

react如何开发组件

react如何开发组件

React 组件开发基础 React 组件分为函数组件和类组件两种形式。函数组件是现代 React 开发的主流方式,结合 Hooks 可以实现完整功能。 函数组件示例: function Gree…

vue VUE实现日历组件功能

vue VUE实现日历组件功能

Vue 日历组件实现方法 基础日历结构 使用Vue的模板语法构建日历的HTML结构,通常包含头部(年月显示、切换按钮)和日历表格(星期标题和日期格子)。 <template> &l…

vue实现拖拽实现组件嵌套

vue实现拖拽实现组件嵌套

实现拖拽组件嵌套的核心思路 Vue中实现拖拽组件嵌套需要结合拖拽库(如vuedraggable)和组件递归渲染。核心在于动态管理嵌套数据结构,并通过事件传递实现层级更新。 安装必要依赖 推荐使用v…

react组件如何通讯

react组件如何通讯

React 组件通讯方式 React 组件间的通讯方式主要包括以下几种方法,适用于不同场景下的数据传递和状态管理需求。 父子组件通讯(Props 传递) 父组件通过 props 向子组件传递数据或回…