全局引入或者局部引入使用
全局引入
import DatePicker from '@/components/datePicker/src/date-picker.js'
Vue.component(DatePicker.name, DatePicker)
局部引入
import DatePicker from '@/components/datePicker'
调用方法,下面的参数和element-ui原有el-date-picker的api保持不变
<date-picker
type="yearrange"
v-model="filter1"
value-format="yyyy"
format="yyyy年"
range-separator="至"
start-placeholder="开始年份"
end-placeholder="结束年份"
></date-picker>
日期限制处理(禁用),下面我以我这边的需求为例,
data () {
return {
timeRange: '',
pickerOptions: {
onPick: ({ maxDate, minDate }) => {
this.timeRange = minDate
if (maxDate) this.timeRange = ''
},
disabledDate: time => {
if (this.timeRange) {
const minTime = this.timeRange - 3
const maxTime = this.timeRange + 3
return (
dayjs(time).format('YYYY') < minTime ||
dayjs(time).format('YYYY') > maxTime ||
time.getTime() + 1 * 24 * 3600 * 1000 > Date.now()
)
} else {
return (
time.getTime() + 1 * 24 * 3600 * 1000 > Date.now()
)
}
}
}
}
}
最终效果
iShot_2023-09-07_14.44.04