当父组件更改prop(boolean)时,子组件不会更新

问题描述 投票:0回答:1

当我更改父Vue组件中的props值(布尔值)时,子组件将不会更新以触发打开模式。

在我的父组件中,click事件将openModal的值从false设置为true。然后将该值通过prop传递给子组件。在该子组件中,更新后的布尔值应该通过类绑定将类添加到div,而div又返回打开模态。

父组件:

<FilmListItem
      v-for="slice in slices"
      @click.native="openModal=true"
    />

<child-component :modal="openModal">
...

data() {
  return {
    openModal: false
  }
}

子组件:

<div
    class="modal__container"
    :class="{ 'modal--show': showModal }">
...
export default {
  props: {
    modal: Boolean
  },
  data() {
    return {
      showModal: this.modal
    }

In the vue dev tools I can see, that the value of the prop changes in the parent. Yet, my child component doesn't update. It worked when I forced the child component to reload when I was assigning a new :key value together with changing the Boolean. But that feels a little hacky to me. Also, a watcher within the child component didn't do the trick. It simply wouldn't watch the changed prop value. Any ideas very much appreciated. Thanks in advance.
javascript vue.js components
1个回答
0
投票

我实际上现在自己找到了解决方案:

在子组件中,我试图通过数据对象访问props的数据。但我只是直接访问道具的数据:

<div   
  :class="{ 'modal--show': modal }"> 
...

export default {   
  props: {
    modal: Boolean 
  } 
}
© www.soinside.com 2019 - 2024. All rights reserved.