【发布时间】:2019-08-27 20:59:02
【问题描述】:
当我更改父 Vue 组件中的 props 值(布尔值)时,子组件不会更新以触发模式打开。
在我的父组件中,单击事件将 openModal 的值从 false 设置为 true。然后这个值通过一个 prop 向下传递给一个子组件。在该子组件中,更新后的布尔值应该通过类绑定添加一个类到一个 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