VueJS инвертированное значение в v-модели - PullRequest
0 голосов
/ 03 апреля 2020

Вот мой код:

<input
   v-model="comb.inactive"
   type="checkbox"
   @click="setInactive(comb.id_base_product_combination)"
>

Мне нужно применить инвертирование comb.inactive к v-модели.

Вот что я попробовал:

<input
    v-model="comb.inactive == 1 ? 0 : 1"
    type="checkbox"
    @click="setInactive(comb.id_base_product_combination)"
>



<input
    v-model="comb.inactive == 1 ? false : true"
    type="checkbox"
    @click="setInactive(comb.id_base_product_combination)"
>

У вас есть другие идеи?

1 Ответ

1 голос
/ 03 апреля 2020

Вы должны сделать следующее:

<input
   v-model="comb.inactive"
   type="checkbox"
   @click="setInactive(comb.id_base_product_combination)"
>
mounted(){
      this.comb['inactive'] = !(this.comb['inactive']);
}

Для лучшей практики вы можете использовать computed:

<input
   v-model="checkedItem"
   type="checkbox"
   @click="setInactive(comb.id_base_product_combination)"
>
computed: {
      checkedItem: {
        get: function () {
          return !this.comb['inactive'];
        },
        set: function (newVal) {
          console.log("set as you want")
        }
}
...