【问题标题】:VueJS Datepicker: dynamically change data based on monthVueJS Datepicker:根据月份动态更改数据
【发布时间】:2019-05-29 10:48:13
【问题描述】:

我是 VueJS 的新手,我正在将 vue-hotel-datepicker 库合并到我的项目中。

目标:根据月份更改 minNights 数据属性。如documentation 中所述,我能够简单地覆盖该值。

问题minNights 在 8 月和 7 月应该是 8。当我使用 Vue 检查器时,我可以看到 HotelDatePicker 组件(从库中导入)有一个 'activeMonthIndex',所以我相信我应该使用它。但是我怎样才能从我的父母那里访问这个数据属性呢?如何根据月份动态更改minNights 属性?

我的模板:

<datepicker
    :startDate="startDate"
    @check-in-changed="setCheckInDate"
    @check-out-changed="setCheckOutDate"
    :maxNights="21"
    :minNights="minNights" //dynamic based on month
    :checkIn="checkIn"
    :checkOut="checkOut"
    :disabledDates="bookedDates"
    :firstDayOfWeek="1"
    :i18n="lang"
    :showYear="true"
 >

我已经为希望查看我的代码的人创建了gist。 非常感谢任何指导。

【问题讨论】:

  • 请问您的具体问题是什么?
  • 我希望根据月份更改 minNights 属性。我编辑了我的问题以使其更清楚。提前谢谢你

标签: javascript vue.js vuejs2 datepicker vue-component


【解决方案1】:

您的 minNights 属性应绑定到 computed 属性,例如:

<datepicker
:startDate="startDate"
@check-in-changed="setCheckInDate"
@check-out-changed="setCheckOutDate"
:maxNights="21"
:minNights="minNights" //dynamic based on month
:checkIn="checkIn"
:checkOut="checkOut"
:disabledDates="bookedDates"
:firstDayOfWeek="1"
:i18n="lang"
:showYear="true"
>

脚本:

  computed:{
        minNights(){

          let currentMonth=this.$children[0]._data.activeMonthIndex;
          if(currentMonth==6 || currentMonth==7){
             return currentMonth;
           }   
         }
     }

【讨论】:

  • 谢谢,但是如何将 minNights 设置为特定月份?我认为,我可以使用的唯一可见属性是我的第二个屏幕截图中的那个(由 HotelDatepicker 库生成的 activeMonthIndex)。我怎样才能访问它/有更好的方法吗?我需要将一个月链接到我的 minNights。
  • 当我检查 code 时,该组件不会发出 activeMonthIndex 的值
  • 那我想知道那个属性是从哪里来的。我只有在 Vue 开发工具中单击 HotelDatePicker 组件时才能看到它。知道如何以另一种方式访问​​该月吗?谢谢。
  • 这种方式不行,你有没有尝试从newCheckIn获取月份?
  • 我知道,但问题是我需要在newCheckin 初始化之前设置minNights...所以我需要在用户开始交互之前设置minNights 属性我的日期选择器。感谢您的帮助,希望我们能找到解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-23
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 2020-04-27
相关资源
最近更新 更多