vue.js - 为什么更改子组件的属性会影响父组件?
问题描述
我的应用程序由一个具有组件的父应用程序组成。我的意图是隔离组件,以便父级(应用程序)和子级(组件)之间的唯一通信是通过
- 道具(父母→孩子)
- 并发出事件(子→父)。
但是,我遇到了意外反应的问题:当我更改子组件中的属性时,它们也会立即在父组件中更新。
我的总体问题是:props 是父数据的副本,还是指针?
具体来说,我将应用程序中定义的数据发送给我的子组件:
<current-incident
@updated-incident="updateAllincidentsFromIncidentForm"
:currentIncident="currentIncident"
/>
currentIncident
在应用程序中定义data()
:
data() {
return {
currentIncident: {
id: null
},
allIncidents: {},
}
},
从组件的角度来看,作为 a 接收的prop
是一个 Object ( ),然后将其分配给在组件 ( )中props: ['currentIncident']
定义的内部对象。data()
mount()
this.incident = this.currentIncident
this.incident
然后在组件中进行修改,并最终用this.$emit()
.
currentIncident
问题是当我在组件中修改时,父的内容会被即时修改incident
。还没有发生$emit()
。
这让我认为prop
通过:currentIncident="currentIncident"
双向绑定传递currentIncident
,在我的理解中,这正是创建 props 和 emits 的原因(以打破这种双向绑定)
解决方案
推荐阅读
- java - 等待 SingleThreadExecutor 在继续 UI 线程执行之前将结果传递给 UI 线程 (RunOnUIThread)。安卓
- android - Can't import Android library
- javascript - Meteor.js doesn't load content of lib folder
- java - How to print 5 random colors from an array of colors
- docker - edgelet_utils... 获取 https://warlibregistry.azurecr.io/v2/iot-edge-engine-simulator/manifests/0.0.1-amd64:未授权:需要身份验证
- python - How to iterate over JSON array?
- c# - How to change the http status code after starting writing to the HttpContext.Response.Body stream in ASP.NET Core?
- wordpress - CF7:如果提交错误,重定向到另一个 URL
- php - 使用 Laravel Image Intervention 调整图片上传大小时出错
- java - 与 ArrayList 匹配值?