最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Vue編寫一個日期選擇器

 更新時間:2023年12月05日 16:31:16   作者:我愛學(xué)習(xí)yq  
這篇文章主要為大家詳細(xì)介紹了如何使用Vue編寫一個簡單的日期選擇器,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

在 Vue 中實現(xiàn)日期選擇器的方法有很多,下面提供一個簡單的實現(xiàn)方法。

首先,在需要使用日期選擇器的組件中引用 Vue 和 date-fns 庫,date-fns 庫是一個輕量級的 JavaScript 時間日期工具庫,可以方便地處理日期的格式化和計算。

import Vue from 'vue'
import { format } from 'date-fns'
 
export default {
  data () {
    return {
      selectedDate: null
    }
  },
  methods: {
    formatDate (date) {
      return format(date, 'yyyy-MM-dd')
    },
    selectDate (date) {
      this.selectedDate = date
    }
  }
}

在模板中使用第三方日期選擇器組件,這里使用了 Element UI 庫中的 DatePicker 組件。同時給 DatePicker 組件綁定 onChange 事件,將選中的日期保存到 data 中的 selectedDate 變量中。

<template>
  <div>
    <el-date-picker
      v-model="selectedDate"
      type="date"
      format="yyyy-MM-dd"
      @change="selectDate(selectedDate)">
    </el-date-picker>
  </div>
</template>

在 methods 中定義 formatDate 方法來格式化日期,并定義 selectDate 方法來處理選擇日期的操作。

import Vue from 'vue'
import { format } from 'date-fns'
 
export default {
  data () {
    return {
      selectedDate: null
    }
  },
  methods: {
    formatDate (date) {
      return format(date, 'yyyy-MM-dd')
    },
    selectDate (date) {
      this.selectedDate = date
    }
  }
}

這樣,一個簡單的 Vue 日期選擇器就完成了,可以根據(jù)實際需要進行擴展和修改。

方法補充

除了上文的方法,小編還為大家整理了其他vue實現(xiàn)日期選擇器的方法,下面是示例代碼,希望對大家有所幫助

完整代碼

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Vue 日期選擇器</title>
  <style>
    .datepicker {
      position: relative;
      display: inline-block;
    }
    
    .datepicker input {
      width: 120px;
      padding: 4px;
      border: 1px solid #ccc;
      outline: none;
      cursor: pointer;
    }
    
    .datepicker-panel {
      position: absolute;
      top: 30px;
      left: 0;
      z-index: 1000;
      border: 1px solid #ccc;
      background-color: #fff;
      box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.15);
    }
    
    .datepicker-content {
      padding: 10px;
    }
    
    .datepicker-header {
      margin-bottom: 10px;
      text-align: center;
    }
    
    .datepicker-header span {
      margin: 0 10px;
      cursor: pointer;
    }
    
    .datepicker-body {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
    }
    
    .datepicker-row {
      display: flex;
    }
    
    .datepicker-cell {
      width: calc(100% / 7);
      text-align: center;
      cursor: pointer;
    }
    
    .datepicker-cell.filled:hover {
      background-color: #ddd;
    }
  </style>
</head>
<body>
<div id="app">
  <date-picker v-model="date"></date-picker>
</div>

<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
  Vue.component('date-picker', {
    template: `
    <div class="datepicker">
      <input type="text" v-model="value" @click="showPicker">
      <div class="datepicker-panel" v-show="visible">
        <div class="datepicker-content">
          <div class="datepicker-header">
            <span class="datepicker-prev-year" @click="prevYear">&lt;&lt;</span>
            <span class="datepicker-prev-month" @click="prevMonth">&lt;</span>
            <span class="datepicker-current-month">{{ currentMonth }}</span>
            <span class="datepicker-next-month" @click="nextMonth">&gt;</span>
            <span class="datepicker-next-year" @click="nextYear">&gt;&gt;</span>
          </div>
          <div class="datepicker-body">
            <div class="datepicker-row" v-for="week in weeks">
              <span class="datepicker-cell"
                    v-for="day in week"
                    :class="{'filled': day !== ''}"
                    @click="selectDate(day)">
                {{ day }}
              </span>
            </div>
          </div>
        </div>
      </div>
    </div>
  `,
    props: {
      value: {
        type: String,
        default: ''
      }
    },
    data() {
      return {
        visible: false,
        year: 0,
        month: 0,
        day: 0
      }
    },
    computed: {
      currentMonth() {
        return `${this.year} 年 ${this.month} 月`
      },
      weeks() {
        return this.getWeeks(this.year, this.month)
      }
    },
    methods: {
      showPicker() {
        this.visible = true
        const date = new Date()
        this.year = date.getFullYear()
        this.month = date.getMonth() + 1
        this.day = date.getDate()
      },
      prevYear() {
        this.year--
      },
      nextYear() {
        this.year++
      },
      prevMonth() {
        if (this.month === 1) {
          this.year--
          this.month = 12
        } else {
          this.month--
        }
      },
      nextMonth() {
        if (this.month === 12) {
          this.year++
          this.month = 1
        } else {
          this.month++
        }
      },
      getWeeks(year, month) {
        const weeks = []
        const firstDay = new Date(year, month - 1, 1).getDay()
        const lastDay = new Date(year, month, 0).getDate()
        let week = []
        for (let i = 0; i < firstDay; i++) {
          week.push('')
        }
        for (let i = 1; i <= lastDay; i++) {
          week.push(i)
          if ((firstDay + i) % 7 === 0) {
            weeks.push(week)
            week = []
          }
        }
        if (week.length > 0) {
          for (let i = 0; i < 7 - week.length; i++) {
            week.push('')
          }
          weeks.push(week)
        }
        return weeks
      },
      selectDate(day) {
        if (day) {
          this.value = `${this.year}-${this.month}-${day}`
          this.visible = false
        }
      }
    }
  })

  new Vue({
    el: '#app',
    data: {
      date: ''
    }
  })
</script>
</body>
</html>

到此這篇關(guān)于使用Vue編寫一個日期選擇器的文章就介紹到這了,更多相關(guān)Vue日期選擇器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue路徑上如何設(shè)置指定的前綴

    vue路徑上如何設(shè)置指定的前綴

    這篇文章主要介紹了vue路徑上如何設(shè)置指定的前綴,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Vue中父子組件通訊之todolist組件功能開發(fā)

    Vue中父子組件通訊之todolist組件功能開發(fā)

    這篇文章主要介紹了Vue中父子組件通訊——todolist組件功能開發(fā)的相關(guān)知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-05-05
  • vue實現(xiàn)簡單數(shù)據(jù)雙向綁定

    vue實現(xiàn)簡單數(shù)據(jù)雙向綁定

    這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)簡單數(shù)據(jù)雙向綁定,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • vue限制輸入框只能輸入8位整數(shù)和2位小數(shù)的代碼

    vue限制輸入框只能輸入8位整數(shù)和2位小數(shù)的代碼

    這篇文章主要介紹了vue限制輸入框只能輸入8位整數(shù)和2位小數(shù),文中我們使用v-model加watch 實現(xiàn)這一個功能,代碼簡單易懂,需要的朋友可以參考下
    2019-11-11
  • vue實現(xiàn)web前端登錄頁數(shù)字驗證碼

    vue實現(xiàn)web前端登錄頁數(shù)字驗證碼

    這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)web前端登錄頁數(shù)字驗證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • elementplus el-table(行列互換)轉(zhuǎn)置的兩種方法

    elementplus el-table(行列互換)轉(zhuǎn)置的兩種方法

    本文主要介紹了elementplus el-table(行列互換)轉(zhuǎn)置,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • Vue3全局API轉(zhuǎn)移的過程詳解

    Vue3全局API轉(zhuǎn)移的過程詳解

    在Vue2到Vue3的升級過程中,除了響應(yīng)式系統(tǒng)、組件寫法等發(fā)生了變化,全局 API 的轉(zhuǎn)移也是一個非常重要的點,很多初學(xué)者在遷移代碼時會遇到報錯,就是因為Vue3不再像Vue2那樣把所有API掛在Vue構(gòu)造函數(shù)上了,本文就來詳細(xì)講解一下Vue的全局API轉(zhuǎn)移,需要的朋友可以參考下
    2025-09-09
  • vue實現(xiàn)自定義表格工具擴展

    vue實現(xiàn)自定義表格工具擴展

    這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)自定義表格工具擴展,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Vue?項目中通過?Vite?實現(xiàn)按需加載功能對比?Webpack?的優(yōu)缺點分析

    Vue?項目中通過?Vite?實現(xiàn)按需加載功能對比?Webpack?的優(yōu)缺點分析

    這篇文章主要介紹了Vue項目中通過Vite實現(xiàn)按需加載功能對比Webpack的優(yōu)缺點分析,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2025-06-06
  • 基于vue封裝一個帶眼睛的密碼子組件

    基于vue封裝一個帶眼睛的密碼子組件

    這篇文章給大家介紹了基于vue封裝一個帶眼睛的密碼子組件的方法,文章中有詳細(xì)的代碼講解,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2023-09-09

最新評論

鲁甸县| 德保县| 吉水县| 仁布县| 西青区| 咸阳市| 梅州市| 扬州市| 井研县| 常山县| 荔浦县| 福安市| 社会| 崇义县| 广宗县| 丁青县| 海兴县| 隆安县| 洞口县| 墨江| 大兴区| 道真| 临夏市| 鹤岗市| 合阳县| 民勤县| 资讯 | 蕲春县| 县级市| 庄河市| 甘孜| 龙川县| 秦皇岛市| 高平市| 新竹县| 湘潭市| 台中县| 盖州市| 新郑市| 宜都市| 合川市|