[英]How can I display value edited in combobox dynamic on the vue component?
I have two component 我有两个组成部分
My first component (parent component) like this : 我的第一个组件(父组件)是这样的:
<template>
<div>
...
<form class="form-horizontal" id="form-profile" @submit.prevent="submitFormProfile">
...
<form-select id="province" name="province" v-model="province" :options="provinces" v-on:triggerChange="changeProvince" :is-required="isRequired" model="1">Province</form-select>
<form-select id="regency" name="regency" v-model="regency" :options="regencies" v-on:triggerChange="changeRegency" :is-required="isRequired" model="1">Regency</form-select>
<form-select id="district" name="district" v-model="district" :options="districts" v-on:triggerChange="changeDistrict" :is-required="isRequired" model="1">District</form-select>
....
</form>
...
</div>
</template>
<script>
export default {
data() {
return {
province: null,
regency: null,
district: null,
}
},
mounted() {
...
this.getUserLogin()
.then((response) => {
let user = response.data
this.province = user.address_list.province
this.regency = user.address_list.regency
this.district = user.address_list.district
})
.catch(error => {
console.log(error)
});
},
...
}
</script>
My second component (child component) like this : 我的第二个组件(子组件)是这样的:
<template>
<div class="form-group">
<label :for="id" class="col-sm-3 control-label"><slot></slot></label>
<div class="col-sm-9">
<select :id="id" :name="name" class="form-control" v-model="selected" v-on:change="applySelected">
<option disabled value="">Please select one</option>
<option v-for="option in options" :value="option.id">{{option.name}}</option>
</select>
</div>
</div>
</template>
<script>
export default {
props: ['value', ...],
data() {
return {
selected: this.value|| ''
}
},
mounted() {
console.log(this.value)
}
...
}
</script>
When the components executed, it will call ajax to get value province, regency, district 当组件执行后,它将调用ajax以获取价值所在的省,摄政,区
If I console.log(response.data)
in the response ajax (parent component), I get the value 如果我在响应ajax(父组件)中使用console.log(response.data)
,则会得到该值
But if I console.log(this.value)
in the second component, I don't find the value. 但是,如果我在第二个组件中使用console.log(this.value)
,则找不到该值。 The value = null
值= null
This seems to happen because when calling child component, the ajax process is not finished yet 这似乎是由于调用子组件时ajax进程尚未完成而引起的
How can I solve this problem? 我怎么解决这个问题?
The problem is you only set selected: this.value || ''
问题是您仅将其设置为selected: this.value || ''
selected: this.value || ''
in your data
when the component is initialized. selected: this.value || ''
在你的data
时,该组件被初始化。 To have it update, you need to add a watcher too, like so: 要更新它,您还需要添加一个观察者,如下所示:
watch: {
value(newValue) {
this.selected = newValue;
}
}
This will update the selected
prop in child each time the one in parent changes. 每当父项中的一项更改时,这将更新子项中的selected
道具。
You can get rid of selected
altogether and just use value
in the child. 您可以完全摆脱selected
而只在子级中使用value
。 It will always remain up to date with the parent. 它将始终与父代保持最新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.