[英]vue js v-model is doesn't change the value in the text input or the value in the data
[英]Vue: changing value of a data property used in v-model doesn't render the newly set value
我目前正在使用Vue與Vuex創建步進器組件。 每個步驟都是一個包含輸入字段的組件。 每個步驟都將輸入字段的值存儲在Vuex存儲中。 進入上一步時,應從商店加載已經可用的數據並顯示在相應的輸入字段中。
我正在使用一個正確實現v-model指令的自定義輸入組件。
<custom-input v-model="amount"
v-bind:type="'number'"></custom-input>
“amount”在數據函數中定義:
data: function () {
return {
amount: null
}
}
現在,我正在嘗試在安裝組件時設置v-model屬性的值。
mounted() {
this.amount = this.$store.state.fields.amount.value;
}
通過調試工具,我可以看到商店保存了正確的值。 組件的數量屬性的情況也是如此。
我也嘗試使用set方法設置屬性,如下所示:
this.$set(this.$data, 'amount', this.$store.state.fields.amount.value);
但它仍然沒有顯示在自定義輸入中。
如何正確設置v-model中使用的數據屬性,使其顯示在輸入字段中?
編輯
自定義輸入組件的實現方式如下:
<input type="'text'" v-on:input="onInput">
onInput: function (event) {
this.$emit('input', event.target.value);
}
問題是我實際上沒有綁定自定義輸入組件中的value屬性。 添加此項修復了問題:
<custom-input ... :value="value" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.