【发布时间】:2021-09-06 21:24:08
【问题描述】:
我不熟悉在 Vue 计算属性中使用 get 和 set 方法。我在计算中有以下代码。
editSmallText: {
get() {
return this.word.translation.smallText.join("");
},
set(value) {
if (typeof value == "string") this.editSmallTextArray = value.split("");
else this.editSmallTextArray = value;
},
},
我使用 this.editSmallText 作为输入的 v-model。我将 this.word 作为组件道具中的对象,并将 this.editSmallTextArray 作为数据中的数组。现在,如果我在输入字段中更改某些内容 this.editSmallTextArray 得到更新,我将更新的值分配给函数内的 this.word.translation.smallText 并将 this.word.translation.smallText 的更新值发送到 firebase 的更新函数,该函数在 firebase 中更新它火库。并将模板中的更新值设为{{ word.translation.smallText }},但在我的输入 v-model 中,我仍然在 editSmallText v-model 中获得 this.word.translation.smallText 的旧值,直到我刷新页面/重新加载组件。我不确定为什么 editSmallText 在 word.translation.smallText 更新时没有得到更新。
<p class="text-h5 q-mb-sm text-center" v-if="!editSmall">
{{ word.translation.smallText.join("") }}
</p>
<q-input
v-model="editSmallText"
class="text-white"
dense
standout
dark
v-else
autogrow
sanitize
/>
<q-btn
icon="edit"
@click="editSmall = !editSmall"
size="sm"
round
flat
dense
class="text-white"
v-if="!editSmall"
/>
<q-btn
icon="save"
class="text-white"
@click="saveEditSmallText()"
size="sm"
v-if="editSmall"
round
flat
dense
/>
props: { word: Object, isSelected: Boolean },
data(){
return {
editSmall: false,
editSmallTextArray: [],
}
}
computed:{
editSmallText: {
get() {
return this.word.translation.smallText.join("");
},
set(value) {
if (typeof value == "string")
this.editSmallTextArray = value.split(",");
else this.editSmallTextArray = value;
},
},
},
methods:{
saveEditSmallText() {
this.editSmall = !this.editSmall;
this.word.translation.smallText = this.editSmallTextArray;
this.edit();
},
edit() {
let payload = {
word: this.word.word,
id: this.$el.id,
updatedAt: new Date(),
smallText: this.word.translation.smallText,
dictionary: this.word.translation.orgTrans.dictionary,
transcription: this.word.translation.orgTrans.transcription,
};
this.$store.dispatch("updateWord", payload);
this.$q.notify({
color: "positive",
textColor: "white",
icon: "update",
position: "top",
message: this.$t("Changes saved successfully!"),
timeout: 3000,
});
},
}
【问题讨论】:
-
您需要添加更多代码...或者更好的是,创建一个Minimal, Reproducible Example
-
@MichalLevý 现在请看一下我添加了更多代码,包括数据和方法。看看能不能帮上忙。谢谢
标签: javascript vue.js vue-component computed-properties vue-reactivity