顺便说一下,在执行此操作时,如何在hasChanged
中保持apiAction
的值?
let apiAction = {}
function firstFunction () {
apiAction.form.hasChanged = true
}
function secondFunction () {
apiAction = {
name: 'updateBuilding',
form: this.buildingForm,
clearFields: true
} // this will remove hasChanged
}
firstFunction()
secondFunction()
答案 0 :(得分:2)
没有特别优雅的方式,因为hasChanged
是form
的属性,而不是apiAction
。
您可以通过form
浅层克隆hasChanged
并将Object.assign
设置为当前值:
secondFunction () {
apiAction = {
name: 'updateBuilding',
form: Object.assign(
{},
this.buildingForm,
{hasChanged: apiAction.form && apiAction.form.hasChanged || false}
),
clearFields: true
}
}
(我假设如果apiAction
没有form
,或form
没有hasChanged
,则该值应为false
。)
...或保留相同的form
对象并设置其属性:
secondFunction () {
if (apiAction.form) {
this.buildingForm.hasChanged = apiAction.form.hasChanged;
}
apiAction = {
name: 'updateBuilding',
form: this.buildingForm,
clearFields: true
}
}