<!-- 日期选择 --> <script name="DateSelect" setup lang="ts"> import dayjs from 'dayjs' const emits = defineEmits(['confirm']) const dateActive = ref('') const dateSelect = ref('') const dateType = ref('month') const selectDate = (type: string) => { dateType.value = type if (type !== 'other') { dateSelect.value = '' } if (type === 'month') { // 获取N月前 const date = dayjs().subtract(1, 'month').format('YYYY-MM-DD HH:mm:ss') dateActive.value = date console.log(date, 'month') } else if (type === 'sixmonth') { const date = dayjs().subtract(6, 'month').format('YYYY-MM-DD HH:mm:ss') dateActive.value = date console.log(date, 'sixmonth') } else if (type === 'year') { const date = dayjs().subtract(1, 'year').format('YYYY-MM-DD HH:mm:ss') dateActive.value = date console.log(date, 'year') } else { dateActive.value = dateSelect.value console.log(dateSelect.value, 'other') } emits('confirm') } selectDate('month') watch(() => dateSelect.value, (newVal) => { if (newVal) { selectDate('other') } }) defineExpose({ dateSelect, dateActive }) </script> <template> <div class="container-date"> <div class="top-date"> <el-button :class="dateType === 'month' ? 'active' : ''" @click="selectDate('month')"> 近一月 </el-button> <el-button :class="dateType === 'sixmonth' ? 'active' : ''" @click="selectDate('sixmonth')"> 近6月 </el-button> <el-button :class="dateType === 'year' ? 'active' : ''" @click="selectDate('year')"> 近一年 </el-button> </div> <div> <el-date-picker v-model="dateSelect" type="date" placeholder="选择日期(自定义)" style="width: 100%;" value-format="YYYY-MM-DD HH:mm:ss" format="YYYY-MM-DD" /> </div> </div> </template> <style lang="scss" scoped> .container-date { position: absolute; // display: flex; z-index: 999; top: 10px; left: 85px; padding: 10px; // width: 150px; background-color: rgba($color: #2f4261, $alpha: 70%); border-radius: 10px; .top-date { // width: 150px; display: flex; justify-content: space-around; margin-bottom: 10px; } } .active { color: #3d7eff; outline: none; background-color: #ecf2ff; border-color: #c5d8ff; } </style>