简体   繁体   English

prop 更新时更新 VueJS 组件数据属性

[英]Updating VueJS component data attributes when prop updates

I'm building a VueJS component which needs to update the data attributes when a prop is updated however, it's not working as I am expecting.我正在构建一个 VueJS 组件,它需要在更新道具时更新数据属性,但是,它并没有像我预期的那样工作。

Basically, the flow is that someone searches for a contact via an autocomplete component I have, and if there's a match an event is emitted to the parent component.基本上,流程是有人通过我拥有的自动完成组件搜索联系人,如果匹配,则将事件发送到父组件。

That contact will belong to an organisation and I pass the data down to the organisation component which updates the data attributes.该联系人将属于一个组织,我将数据传递给更新数据属性的组织组件。 However it's not updating them.但是,它不会更新它们。

The prop being passed to the organisation component is updated (via the event) but the data attibute values is not showing this change.传递给组织组件的道具已更新(通过事件),但数据属性values未显示此更改。

This is an illustration of my component's structure...这是我的组件结构的插图...

在此处输入图像描述

Here is my code...这是我的代码...

Parent component父组件

    <template>
        <div>
            <blink-contact
                    :contact="contact"
                    v-on:contactSelected="setContact">
            </blink-contact>

            <blink-organisation 
                    :organisation="organisation" 
                    v-on:organisationSelected="setOrganisation">
            </blink-organisation>
        </div>
    </template>

    <script>
        import BlinkContact from './BlinkContact.vue'
        import BlinkOrganisation from './BlinkOrganisation.vue'

        export default {
            components: {BlinkContact, BlinkOrganisation},

            props: [
                'contact_id', 'contact_name', 'contact_tel', 'contact_email',
                'organisation_id', 'organisation_name'
            ],

            data () {
                return {
                    contact: {
                        id: this.contact_id,
                        name: this.contact_name,
                        tel: this.contact_tel,
                        email: this.contact_email
                    },

                    organisation: {
                        id: this.organisation_id,
                        name: this.organisation_name
                    }
                }
            },

            methods: {
                setContact (contact) {
                    this.contact = contact
                    this.setOrganisation(contact.organisation)
                },

                setOrganisation (organisation) {
                    this.organisation = organisation
                }
            }
        }
    </script>

Child component (blink-organisation)子组件(闪烁组织)

        <template>
        <blink-org-search
                field-name="organisation_id"
                :values="values"
                endpoint="/api/v1/blink/organisations"
                :format="format"
                :query="getQuery"
                v-on:itemSelected="setItem">
        </blink-org-search>
    </template>

    <script>
        export default {
            props: ['organisation'],

            data() {
                return {
                    values: {
                        id: this.organisation.id,
                        search: this.organisation.name
                    },
                    format: function (items) {
                        for (let item of items.results) {
                            item.display = item.name
                            item.resultsDisplay = item.name
                        }
                        return items.results
                    }
                }
            },

            methods: {
                setItem (item) {
                    this.$emit('organisationSelected', item)
                }
            }
        }
    </script>

How can I update the child component's data properties when the prop changes?道具更改时如何更新子组件的数据属性?

Thanks!谢谢!

Use a watch .使用手表

watch: {
    organisation(newValue){
        this.values.id = newValue.id
        this.values.search = newValue.name
    }
}

In this case, however, it looks like you could just use a computed instead of a data property because all you are doing is passing values along to your search component.但是,在这种情况下,您似乎可以只使用计算属性而不是数据属性,因为您所做的只是将值传递给您的搜索组件。

computed:{
    values(){
        return {
            id: this.organisation.id
            search: this.organisation.name
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM