在做关于数据添加的方法时,需要取到当前的的id号来添加内容,而以下的this.books.some的function中的this指向的windows。
methods: {
// 添加图书
handle: function() {
this.books.some(function(item) {
if (item.id = this.id) {
item.name = this.name;
//完成更新操作后需要终止循环
return true;
}
});
}
}
所以需要取到当前的id值可以用传统的方式,在外部定义 var that=this
或者使用箭头函数也可以直接实现,由于箭头函数的this 指的是父级作用域的this。
methods: {
// 添加图书
handle: function() {
this.books.some((item) => {
if (item.id = this.id) {
item.name = this.name;
//完成更新操作后需要终止循环
return true;
}
});
}
}