[英]Vue.js How to Access Elements of an Object
我有一個對象數組:
data: function() {
return {
customers:[],
}
},
填充此 select 框:
<label>DSO Affiliation:</label>
<select class="select-box form-control" name="customer" id="customer" v-model='customer_id' style="-webkit-appearance: none;">
<option value="" selected>Choose Customer</option>
<option v-for="customer in customers" :value="customer.id">
{{ customer.customer_name }}
</option>
</select>
選擇客戶后,我需要從所選 object 中獲取客戶數據,以便填充其他表單元素,例如:
<label>Customer Address:</label>
<input type="text" class="form-control" name="cust_address" v-model='cust_address'>
我在customers:[ ]
數組中有數據。 如何在不額外訪問服務器的情況下獲取在 select 框中選擇的客戶數據?
通過過濾customers
數組來查看customer_id
並更新cust_address
:
data: function() {
return {
customers:[],
}
},
watch:{
customer_id(val){
let found=this.customers.find(cust=>cust.id===val);
this.cust_address=found?found.address:'';
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.