Vue Monthly Picker Component
Vue Monthly Picker (current version 0.2.8) is a specialized Vue 2 component designed for selecting a specific month and year. It relies on Moment.js for all underlying date manipulation and formatting, specifically requiring Moment.js version ^2.18.1. The package maintains a slow and sporadic release cadence, with recent updates focusing on minor feature enhancements like `clearOption`, `alignment`, and `selectedBackgroundColor` props. Its core functionality offers a simple, dedicated user interface for month-only selection, differentiating it from full date pickers. Key features include customizable month labels, min/max date constraints, and flexible display formatting. This component is suitable for projects firmly established on Vue 2 and Moment.js, but users should be aware of the legacy status of its core dependencies.
Common errors
-
[antdv:DatePicker] value provides invalidate moment time, if you want to set empty value, use null instead.
cause The `v-model` or `value` prop was initialized or updated with an invalid or non-Moment.js date object.fixEnsure the bound `v-model` or `value` prop is always a valid Moment.js object, or `null` for an empty state. For example, `moment('2023-01', 'YYYY-MM')` or `moment()`. -
TypeError: Cannot read properties of undefined (reading '$moment') / 'moment is not a function'
cause Moment.js is not correctly imported, globally available, or passed to the component, or the component is used in a Vue 3 context.fixVerify that Moment.js is installed (`npm install moment`) and correctly imported where `VueMonthlyPicker` is used or globally (`import moment from 'moment';`). Ensure the project is running Vue 2.x. -
Error in render: 'v-model' directives cannot update the value of a non-form control type input. Did you mean to use a two-way data binding on a component prop?
cause This error is unlikely with `vue-monthly-picker` as it explicitly supports `v-model`. However, it can occur if Vue's `v-model` is incorrectly applied to a raw HTML element that doesn't natively support it, or if there's a misconfiguration in how the component's `value` prop and `input` event (or equivalent) are handled internally, or if trying to use Vue 3's `v-model` behavior on a Vue 2 component.fixEnsure `v-model` is correctly implemented by the component (which `vue-monthly-picker` does). If you encounter this, double-check your Vue version and component setup. It might also be a symptom of trying to use a Vue 2 component in a Vue 3 project.
Warnings
- deprecated This package is built for Vue 2, which has reached its End of Life (EOL) for regular support and is now in maintenance. It is not compatible with Vue 3.
- deprecated The component relies heavily on Moment.js, a legacy project that is no longer under active development. While widely used, Moment.js can contribute significantly to bundle size and is generally discouraged for new JavaScript projects in favor of modern alternatives like Luxon, `date-fns`, or native `Intl` APIs.
- gotcha The `v-model` binding and the `value`, `min`, `max` props require a Moment.js object. Passing a native JavaScript `Date` object or a date string will lead to unexpected behavior or errors.
- gotcha Moment.js can sometimes emit deprecation warnings, especially when parsing non-standard date strings or when its internal fallback mechanisms are triggered.
Install
-
npm install vue-monthly-picker -
yarn add vue-monthly-picker -
pnpm add vue-monthly-picker
Imports
- VueMonthlyPicker
const VueMonthlyPicker = require('vue-monthly-picker')import VueMonthlyPicker from 'vue-monthly-picker'
- Vue.component registration
Vue.component('monthly-picker', VueMonthlyPicker); - Moment object for v-model
data() { return { selectedMonth: moment() } }
Quickstart
import Vue from 'vue';
import VueMonthlyPicker from 'vue-monthly-picker';
import moment from 'moment';
Vue.component('my-monthly-picker', VueMonthlyPicker);
new Vue({
el: '#app',
data() {
return {
selectedMonth: moment(), // Initialize with a moment object
minDate: moment().subtract(1, 'year'),
maxDate: moment().add(1, 'year')
}
},
template: `
<div id="app">
<p>Selected Month: {{ selectedMonth ? selectedMonth.format('YYYY-MM') : 'None' }}</p>
<my-monthly-picker
v-model="selectedMonth"
:min="minDate"
:max="maxDate"
placeHolder="Select a month..."
:clearOption="true"
dateFormat="MMMM YYYY"
/>
<p>Choose a month from the picker above.</p>
</div>
`
});