我有一个奇怪的js/vuejs问题,当一个变量在函数内部更新(从data(){})中获取数据时,数据也会随之更新。如何从更新数据变量停止vuejs方法变量
这是一个代码示例。它检查如果用户没有轮廓的图像,它会通过爱可信的形象名称发送到一个API,如果用户已经映像,它可以将当前图像添加到列表,并将其发送到API:
addImage (file) {
var URL = 'http://foo.com/api';
var images = this.person.images;
let att = []
if(images.length) {
att = images
att.push(file.name)
}else{
att.push(file.name)
}
axios.post(URL, {
attachements: att
}).then(
// update dom
).catch(err => {})
},
这里的问题是this.person.images
得到更新,即使我只是想用它来获取当前的图像列表。
如果在执行att.push(file.name)
时检查if/else块,this.person.images;
也会更新。我仅使用att
来存储当前的图像列表并将其发送给api。
无论如何只能使用this.person.image
来获取信息?
我可以使用const att = this.person.image
,但之后我无法更新att
。
我很惊讶这是它的唯一解决方案。我曾想过这样做,但我认为可能有更好的方法。在那儿? – hidar
看看这个数组函数在Javascript中,它可能是你的问题的解决方案。 https://www.w3schools.com/jsref/jsref_copywithin.asp – Tarek
var images = JSON.parse(JSON.stringify(this.person.images))。使用这个简单的解决方案 –