【问题标题】:Vue update stateVue 更新状态
【发布时间】:2021-06-29 13:50:31
【问题描述】:

我在使用 Vue 更新状态时遇到问题。这是一个非常简单的问题,所以我不会发布完整的代码,因为它有很多。 所以问题是我正在使用 websockets 更新一些数据,直到现在一切正常,但是现在,当我试图将这些数据作为状态获取时,它并没有实时更新。 例如,我有一个如下所示的表单:

    props: ['order'],
    data() {
        return {
            client_form: {
                order_id: this.$route.params.id,
                client_id: this.order.client.id,
                fullName: this.order.client.fullName,
                email: this.order.client.email,
                phone: this.order.client.phone,
                city: this.order.client.shipping.city,
                address: this.order.client.shipping.address,
                zip: this.order.client.shipping.zip,
                province: this.order.client.shipping.province,
                comment: this.order.comment
            }
        }
    }

如您所见,我正在获取道具订单,并且它正在从父组件更新。 因此,如果我在模板中调用例如 {{ order.client.fullName }},它会实时更新,但是当我尝试使用 {{ fullName }} 时,它不会更新。

我应该怎么做才能更新这些状态?

【问题讨论】:

    标签: javascript vue.js


    【解决方案1】:

    client_form 定义为计算属性,以便观察路由和道具的变化并返回新值:

     props: ['order'],
     computed:{
        client_form() {
            return {
                    order_id: this.$route.params.id,
                    client_id: this.order.client.id,
                    fullName: this.order.client.fullName,
                    email: this.order.client.email,
                    phone: this.order.client.phone,
                    city: this.order.client.shipping.city,
                    address: this.order.client.shipping.address,
                    zip: this.order.client.shipping.zip,
                    province: this.order.client.shipping.province,
                    comment: this.order.comment
                }
            }
    }
    

    然后在模板中执行:{{client_form.fullName}}

    【讨论】:

    猜你喜欢
    • 2019-08-06
    • 1970-01-01
    • 2022-01-22
    • 2022-07-16
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    • 2021-01-10
    • 2019-01-25
    相关资源
    最近更新 更多