javascript - 对对象属性的更改未反射(reflect)在 Vue 组件中

标签 javascript vue.js vuejs2

我有一个带有单个 prop 的 Vue 组件,它是一个对象。当此对象中的属性更改时,它似乎不会在 Vue 模板中更新。

这是该组件的简化版本:

<template>
  <p>{{ item.quantity }}</p>
</template>

<script>
  export default {
    props: {
      item: {
        required: true,
        type: Object
      }
    }
  }
</script>

使用 vue-devtools在 Google Chrome 中,我可以看到 item 对象中的 quantity 属性正在被更新,但模板仍然只呈现默认值(1).

我需要做些什么来让 Vue 监视嵌套属性或其他什么吗?

最佳答案

问题是组件不知道它应该在 props 发生变化时重新渲染。您可以使用返回 props 值的计算属性并使用计算属性值。

<template>
  <p>{{ quantity }}</p>
</template>

<script>
  export default {
    props: {
      item: {
        required: true,
        type: Object
      }
    }

    computed: {
        quantity: function() {
            return this.item.quantity
        }
    }
  }
</script>

关于javascript - 对对象属性的更改未反射(reflect)在 Vue 组件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49152508/

相关文章:

javascript - 带有 ng-repeat 的 Div 在单选按钮后消失

JavaScript JQuery Ajax 问题 : POST Works fine in Firefox, IE、Safari 但不是 Chrome

javascript - 在 laravel 6.x 中渲染多个 vue 组件

javascript - twbs-pagination 起始页选项不正确

javascript - 绘制折线图 - 多条线

css - 使用嵌套选择器和 vuejs CSS 模块

javascript - 在 vuejs 中保护子路由

vue.js - 如何修复 Stripe API 错误 : "IntegrationError: We could not retrieve data from the specified Element." in a Vue component?

javascript - 在 vuejs 应用程序中集成 openlayers

javascript - 视觉 : convention for conditional props in dynamic components?