Vuejs - 父数组发生变化,但孩子没有看到道具变化

问题描述

我的父母有这个:

{{ fencing }}
<FencingTable
   v-if="fencing.length > 0"
   :fencing="fencing"
   :facility="facility"
/>
get fencing() {
   return this.$store.state.fencing;
}

我的孩子有这个:

<template>
   <div>
   {{fencing}}
...
export default class FencingTable extends Vue {
   apiLocation = Vue.prototype.$apiLocation;
   @Prop() fencing: Fencing[] | null = null;
   @Prop() facility: Facility | null = null;
      ...
}

当我更新我的商店并将第一个项目添加到数组时,我看到父项呈现该项目,但子项显示一个空数组。如果我重新加载页面,一切正常,随后添加到数组的内容就会正确显示

当第一个项目添加到数组时,如何让我的孩子正确更新?

解决方法

来自 vue-property-decorator guide

不支持定义每个默认属性,如@Prop() prop = 'default value'

换句话说,不要使用 = 以这种方式指定默认值,而是:

@Prop({ default: null }) fencing: Fencing[] | null;
@Prop({ default: null }) facility: Facility | null;