gpt4 book ai didi

javascript - Vue Js 所有赋值都是通过引用,即使是传播对象

转载 作者:搜寻专家 更新时间:2023-10-30 22:49:15 24 4
gpt4 key购买 nike

如果模板是:

<div id="app">
<button @click="doEditing">Edit</button>

<input v-if="editing" v-model="editing.profile.name" />
<span>{{ user.profile.name }}</span>
</div>

而 Vuejs 应用是:

var app = new Vue({
el: '#app',
data: {
user: {name: 'Foo'},
editing: null
},
methods: {
doEditing(){
this.editing = {...this.user};
}
}
})

甚至 this.editing = {...this.user}; 正在传播对象,通过引用分配它。因此,当我编辑输入时,v-model 更改了 this.user,这不是我所期望的。

我也测试过 Object.assign,但没有机会。

最佳答案

lodash _.cloneDeep 函数正好解决了这个问题。

关于javascript - Vue Js 所有赋值都是通过引用,即使是传播对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58087208/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com