【发布时间】:2017-10-04 09:25:56
【问题描述】:
我有一个奇怪的 js/vuejs 问题,当一个变量在函数内更新时(从 data(){} 获取数据),它会上升数据也更新它。
这是一个代码示例。它检查用户是否没有头像,它将图像名称通过 axios 发送到 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。
【问题讨论】:
标签: javascript vue.js