A
A
Aleksandr2018-04-25 15:36:28
Vue.js
Aleksandr, 2018-04-25 15:36:28

How to add computed to v-model?

Here is a period selection.
Click on it and the date should be filled in the datepicker

<input v-model="selectedPeriod"
            :id="period.id" :value="period.id"
            type="radio" name="period"
            class="policy-form__label-radio" />

Here is the datepicker
<el-date-picker
          v-model="dateStart"
          type="date"
          :clearable="false"
          placeholder="Выберите дату">
</el-date-picker>

Here is the date
data: () => ({
    periodStart: new Date(),
    periodEnd: new Date(),
    selectedFilter: "all",
    selectedPeriod: "today"
  }),

But what lies in computed
dateStart() {
      return moment().subtract(1, this.selectedPeriod).toDate()
    }

This part of the code works. that is, I click on the periods and the datepicker changes
But now, when I want to change the date in the datepicker itself, it writes that
Computed property "dateStart" was assigned to but it has no setter

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2018-04-25
@Sashjkeee

For computed properties, you can do (and you need to in this situation) and setters:

dateStart: {
  get() {
    return moment().subtract(1, this.selectedPeriod).toDate();
  },
  set(value) {
    // ну, тут вам виднее, какой должен код быть
  },
},

E
Evgeny Kulakov, 2018-04-25
@kulakoff

Well, as if they hint quite clearly that there is an assignment to dateStart, but the setter is undefined, there should be something like:

dateStart: {
      get: function () {
       return moment().subtract(1, this.selectedPeriod).toDate()
      },
      set: function (v) {
        // set here
      }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question