当前位置:首页 > 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() // 禁用过去的日期
  }
}

样式优化建议

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

vue实现日期组件

.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;
}

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

相关文章

uniapp组件写法

uniapp组件写法

Uniapp 组件的基本写法 Uniapp 的组件写法与 Vue 类似,支持单文件组件(.vue 文件)。一个基本的组件通常包含三个部分:<template>、<script>…

vue动态组件实现

vue动态组件实现

动态组件的基本用法 在Vue中,动态组件通过<component>标签和is属性实现。is属性可以绑定组件名称或组件选项对象,实现动态切换。 <template> <…

vue实现组件缓存

vue实现组件缓存

Vue 组件缓存的实现方法 在 Vue 中实现组件缓存通常使用 <keep-alive> 内置组件,它可以缓存不活动的组件实例,避免重复渲染和销毁。 基本用法 <keep-aliv…

vue组件实现computed

vue组件实现computed

Vue 组件中实现 computed 的方法 在 Vue 组件中,computed 用于声明计算属性,根据依赖的响应式数据动态计算并返回结果。计算属性具有缓存机制,只有当依赖的数据发生变化时才会重新计…

vue组件实现跳转

vue组件实现跳转

Vue 组件实现跳转的方法 在 Vue 中实现页面跳转可以通过多种方式,以下是常见的几种方法: 使用 router-link 组件 router-link 是 Vue Router 提供的组件,用于…

vue实现组件复制

vue实现组件复制

vue实现组件复制的方法 使用v-for指令 通过v-for循环生成多个相同结构的组件,适用于需要批量生成相似组件的场景。 <template> <div> &l…