data() {
return {
petOptions: [{id: ''}],
}
},
错误方法 此时的this指向的不是vue
……
.then((response) => {
var state = response.data.state;
if (state) {
this.scarts = response.data.scarts;
this.scarts.forEach(function (item, index) {
let id= item.id;
console.log(di)
this.petOptions[index] = {id: id};
});
……
},
在请求数据前 加一行 var that = this;这样可以正常保存。
var that = this;
……
.then((response) => {
var state = response.data.state;
if (state) {
this.scarts = response.data.scarts;
this.scarts.forEach(function (item, index) {
let id= item.id;
console.log(di)
that.petOptions[index] = {id: id};
});
……
},