Vue Month Picker
vue-month-picker is a lightweight, dependency-free month picker component designed specifically for Vue 3 applications. Currently at version 2.0.0, it offers robust functionality for selecting single months or month ranges without external dependencies, making it a highly performant choice. The library features full TypeScript support, leveraging Vue's Composition API and `<script setup>` syntax for modern Vue development. Key capabilities include inline and input-driven picker modes, date constraints (`minDate`, `maxDate`), customizable date formatting, a dark theme option, year-only selection, and extensive internationalization with over 45 built-in languages. Its focus on Vue 3 and zero dependencies differentiates it from more general-purpose date pickers.
Common errors
-
[Vue warn]: Failed to resolve component: MonthPicker
cause The component was not properly imported or registered in the Vue application's component options or setup context.fixEnsure you have either imported the component locally (e.g., `import { MonthPicker } from 'vue-month-picker'`) and added it to your component's `components` option (or `<script setup>`), or globally registered the plugin (`app.use(VueMonthPicker)`). -
TypeError: Cannot read properties of undefined (reading 'month')
cause Attempting to access properties of the date result object (e.g., `date.month`) when no month has been selected, and the variable is still `null` or `undefined`.fixAdd conditional rendering or null checks before accessing properties of the date object returned by the component (e.g., `<p v-if="date">{{ date.month }}</p>`). This is especially relevant when using the `no-default` prop.
Warnings
- breaking This library is exclusively designed for Vue 3 applications. Attempting to use it in a Vue 2 project will lead to compatibility errors and runtime failures.
- gotcha When defining 'minDate' or 'maxDate' props, ensure you provide a valid JavaScript Date object. Remember that JavaScript Date constructor months are 0-indexed (January is 0), while 'default-month' prop expects a 1-indexed month (January is 1).
- gotcha Combining the 'no-default' prop with 'default-month' or 'default-year' can lead to unexpected behavior. 'no-default' explicitly prevents any initial selection.
Install
-
npm install vue-month-picker -
yarn add vue-month-picker -
pnpm add vue-month-picker
Imports
- MonthPicker
import MonthPicker from 'vue-month-picker'
import { MonthPicker } from 'vue-month-picker' - MonthPickerInput
import MonthPickerInput from 'vue-month-picker'
import { MonthPickerInput } from 'vue-month-picker' - IDateResult
import { IDateResult } from 'vue-month-picker'import type { IDateResult } from 'vue-month-picker' - VueMonthPicker (Plugin)
import { VueMonthPicker } from 'vue-month-picker'import VueMonthPicker from 'vue-month-picker'
Quickstart
<script setup lang="ts">
import { MonthPicker, MonthPickerInput } from 'vue-month-picker'
import type { IDateResult } from 'vue-month-picker'
const onDateChange = (date: IDateResult) => {
console.log('Selected Month:', date.month, 'Year:', date.year)
}
const onRangeChange = (dateRange: { month: number; year: number }[]) => {
console.log('Selected Range:', dateRange)
}
</script>
<template>
<h3>Inline Month Picker</h3>
<MonthPicker @change="onDateChange" />
<h3>Input Month Picker</h3>
<MonthPickerInput
:no-default="true"
date-format="%n %Y"
placeholder="Select a month"
@change="onDateChange"
/>
<h3>Range Selection Picker</h3>
<MonthPicker
:range="true"
:no-default="true"
@change="onRangeChange"
/>
</template>