Vue 可以直接绑定原生 HTML5 的月份选择器,简单高效:
当前位置:首页 > VUE

vue实现选择月份

2026-01-15 05:42:14VUE

使用 Vue 实现选择月份功能

方法一:使用原生 HTML <input type="month">

Vue 可以直接绑定原生 HTML5 的月份选择器,简单高效:

<template>
  <input type="month" v-model="selectedMonth" @change="handleMonthChange">
</template>

<script>
export default {
  data() {
    return {
      selectedMonth: ''
    }
  },
  methods: {
    handleMonthChange() {
      console.log('Selected month:', this.selectedMonth); // 格式为 YYYY-MM
    }
  }
}
</script>

注意:部分老旧浏览器可能不支持此类型输入框。

方法二:使用第三方组件库

Element UI 的日期选择器支持月份选择模式:

<template>
  <el-date-picker
    v-model="selectedMonth"
    type="month"
    placeholder="选择月份"
    format="YYYY-MM"
    value-format="YYYY-MM">
  </el-date-picker>
</template>

<script>
export default {
  data() {
    return {
      selectedMonth: ''
    }
  }
}
</script>

Ant Design Vue 的实现方式:

<template>
  <a-month-picker v-model:value="selectedMonth" format="YYYY-MM" />
</template>

<script>
import { ref } from 'vue';
export default {
  setup() {
    const selectedMonth = ref(null);
    return { selectedMonth };
  }
}
</script>

方法三:自定义月份选择组件

如需完全自定义样式和功能,可以手动实现:

<template>
  <div class="month-picker">
    <select v-model="selectedYear">
      <option v-for="year in years" :key="year" :value="year">{{ year }}</option>
    </select>
    <select v-model="selectedMonth">
      <option v-for="month in 12" :key="month" :value="month">
        {{ month }}月
      </option>
    </select>
  </div>
</template>

<script>
export default {
  data() {
    const currentYear = new Date().getFullYear();
    return {
      years: Array.from({length: 10}, (_, i) => currentYear + i),
      selectedYear: currentYear,
      selectedMonth: new Date().getMonth() + 1
    }
  },
  watch: {
    selectedYear() {
      this.$emit('change', `${this.selectedYear}-${this.selectedMonth}`);
    },
    selectedMonth() {
      this.$emit('change', `${this.selectedYear}-${this.selectedMonth}`);
    }
  }
}
</script>

<style>
.month-picker {
  display: flex;
  gap: 10px;
}
</style>

方法四:使用 Vue Date Picker 库

安装 vue-datepicker-next 库:

npm install vue-datepicker-next

使用示例:

vue实现选择月份

<template>
  <date-picker
    v-model="selectedMonth"
    type="month"
    format="YYYY-MM"
    value-type="format"
  />
</template>

<script>
import DatePicker from 'vue-datepicker-next';
import 'vue-datepicker-next/index.css';

export default {
  components: { DatePicker },
  data() {
    return {
      selectedMonth: ''
    }
  }
}
</script>

每种方法适用于不同场景,原生输入最简单但兼容性有限,组件库提供丰富功能,自定义组件灵活性最高。根据项目需求选择合适方案。

标签: vue
分享给朋友:

相关文章

vue实现a

vue实现a

Vue 实现 A 功能的方法 在 Vue 中实现 A 功能可以通过多种方式,具体取决于 A 功能的具体需求。以下是几种常见的实现方法: 使用 Vue 组件 通过创建自定义组件来实现 A 功能。组件可…

vue实现新闻

vue实现新闻

Vue 实现新闻功能 使用 Vue 实现新闻功能通常涉及新闻列表展示、新闻详情页、分类筛选等模块。以下是实现的基本思路和代码示例。 新闻列表展示 通过 Vue 组件展示新闻列表,通常使用 v-for…

vue实现引导

vue实现引导

Vue 实现引导功能的方法 使用第三方库 推荐使用 driver.js 或 intro.js 这类专门为 Vue 设计的引导库。这些库提供了完整的 API 和样式,能够快速实现引导功能。 安装 dr…

vue实现键盘

vue实现键盘

Vue 实现键盘功能 在Vue中实现键盘功能通常涉及监听键盘事件、处理输入以及渲染虚拟键盘界面。以下是几种常见的实现方式: 监听键盘事件 通过@keydown或@keyup指令监听键盘事件,适用于需…

vue 实现回复

vue 实现回复

Vue 实现回复功能 在 Vue 中实现回复功能通常涉及表单提交、数据绑定和列表渲染。以下是实现步骤和代码示例: 数据绑定与表单 <template> <div>…

vue 实现拖动

vue 实现拖动

Vue 实现拖动的几种方法 在Vue中实现拖动功能可以通过多种方式,包括原生HTML5的拖放API、第三方库如vuedraggable等。以下是几种常见的实现方法: 使用HTML5拖放API HTM…