简体   繁体   English

如何在vue组件上显示在combobox动态中编辑的值?

[英]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? 我怎么解决这个问题?

Option 1 - Add a watcher 选项1-添加观察者

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道具。

Option 2 - Refer directly to the prop 选项2-直接参考道具

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.

相关问题 在Vue组件上编辑数据时如何显示输入类型日期的值? - How can I display value of input type date when edit data on vue component? 如何在vue组件中显示所需的html 5? - How can I display required html 5 in vue component? 如何使用组件中的方法更改 vue 中 props 的值? - How can I change the value of props in vue with a method in a component? 在 vue 组件上提交表单时如何获得价值? - How can I get value when submit form on the vue component? 如何根据 php 中 combobox 中的选定值在数据库中的文本框中显示该值? - How can I display value in textbox from database that value based on selected value from combobox in php? 如何基于字符串的值创建嵌套的动态组件? - How can I create nested dynamic component based on value of string? 在我单击 Vue Devtools 中的组件之前,计算值不会显示 - Computed value not display until I click component in Vue Devtools 如何在 Vue Js 中将数据值从一个组件更改为另一个组件? - How can I change data value from one component to another component in Vue Js? 如何在vue3组件测试中修改pinia中state的值并影响组件? - How can I modify the value of state in pinia in vue3 component test and affect the component? 在动态 Vue 组件中进行不同的显示 - Make a different display in a dynamic Vue component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM