簡體   English   中英

如果項目已更新,則從 v-select 更新 v-model

[英]Update v-model from v-select if items are updated

Vue模板:

<v-flex>
 <v-select
   label="Day"
   :items="daysInMonth"
   v-model="selectedDay"
  />
</v-flex>
<v-flex>
  <v-select
   label="Month"
   :items="months"
   v-model="selectedMonth"
  />
</v-flex>
<v-flex>
  <v-select
   label="Years"
   :items="years"
   v-model="selectedYear"
  />
</v-flex>

我正在使用v-select開發日期選擇器組件。 它工作正常,但我不理解我有一個案例來評估一個月/一年中的天數的行為。

所以daysInMonth是一個計算屬性,它將返回給定selectedMonthselectedYear的天數。

示例情況, daysInMonth[1, 2, 3, ..., 29]並且selectedDay29 ,但是在daysInMonth的新評估之后,即說[1, 2, 3, ..., 28] ,我的selectedDay數據值被保留,即在 Vue 調試工具上仍然顯示29 ,但在 UI 上沒有。

我在這里想念什么? 如果項目中不存在該值,我希望更新selectedDay值,即daysInMonth

謝謝!

您可以在daysInMonth上使用watch

watch: {
  daysInMonth(val) {
    this.selectedDay = val.includes(this.selectedDay) ? this.selectedDay : 1;
  }
}

如果 selectedDay 不在新的daysInMonth數組中,這會將selectedDay重置為 1。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM