admin管理员组文章数量:1315823
I have a view that stores items in a reservation liste. The reservation list sits in a store.
this is a vuetify switch
<v-switch
hide-details
inset
color="black"
:value="isReserved"
@change="toggleReservation"
:disabled="isReserveDisabled"></v-switch>
v-card-actions>
this is the computed:
isReserved() {
return equipmentStore.reservations.some(
item => item.id === this.id
);
},
clicking the switch will add and remove id's to the equipmentStore.reservations array. This works but the state of the button is not reflecting that. It stays in to off position.
If we change it to
<v-switch
hide-details
inset
color="black"
v-model="isReserved"
@change="toggleReservation"
:disabled="isReserveDisabled"></v-switch>
it does work but there is the obivous warning in the console that the computed is readonly.
What would be the proper way to reflect the state of this button based on a value in the store?
I have a view that stores items in a reservation liste. The reservation list sits in a store.
this is a vuetify switch
<v-switch
hide-details
inset
color="black"
:value="isReserved"
@change="toggleReservation"
:disabled="isReserveDisabled"></v-switch>
v-card-actions>
this is the computed:
isReserved() {
return equipmentStore.reservations.some(
item => item.id === this.id
);
},
clicking the switch will add and remove id's to the equipmentStore.reservations array. This works but the state of the button is not reflecting that. It stays in to off position.
If we change it to
<v-switch
hide-details
inset
color="black"
v-model="isReserved"
@change="toggleReservation"
:disabled="isReserveDisabled"></v-switch>
it does work but there is the obivous warning in the console that the computed is readonly.
What would be the proper way to reflect the state of this button based on a value in the store?
Share Improve this question edited Jan 30 at 10:15 DarkBee 15.6k8 gold badges72 silver badges117 bronze badges asked Jan 30 at 10:14 theking2theking2 2,8652 gold badges34 silver badges48 bronze badges 1 |1 Answer
Reset to default 3This Warning occurred due to you use computed property.which means it cannot be modified directly.
we can use another way Getter(get()) and Setter(set()) methods as below.
just update computed hook as below snippet code:
computed: {
isReserved: {
get() {
return equipmentStore.reservations.some(item => item.id === this.id);
},
set(value) {
if (value) {
// Add the item to reservations if not already present
if (!this.isReserved) {
equipmentStore.reservations.push({ id: this.id });
}
} else {
// Remove the item from reservations
equipmentStore.reservations = equipmentStore.reservations.filter(
item => item.id !== this.id
);
}
}
}
}
and also update a v-switch code in template tag as below:
<v-switch
hide-details
inset
color="black"
v-model="isReserved"
:disabled="isReserveDisabled"></v-switch>
It's directly update your store. so we don't need to update v-modal value.
I hope it's working for you.
本文标签: vuejsproper way to compute a value based on a storeStack Overflow
版权声明:本文标题:vue.js - proper way to compute a value based on a store? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741973589a2407987.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
@change
together indicates that something went wrong, it's either one or another. v-model stands for:model-value
, not:value
prop. Probably this is what's needed here – Estus Flask Commented Jan 30 at 10:45