问题描述:
通过项目ID向后端请求数据,数据请求成功后会向数组中push内容,通过watch监听项目ID变化后重新执行这个方法,但接口会执行两遍,数组中也会被push两次,在请求中清空了数组但依然会push两次内容,所以页面显示也会出错
原代码如下:
// data中定义了一个空数组studentData,请求到数据后向这个数组中push数据
data() {
return {
studentData: [],
}
}
created() {
this.getStudentData();
}
methods: {
// 请求数据的方法,通过项目ID请求数据
getStudentData() {
this.studentData = [];
let params = {
ProjectId: this$route.query.projectId,
}
countStudent(params, res => {
this.studentData.push({
title: '未关联学生',
tips: '已关联学生数量',
contentValueO: '已关联',
contentValueT: '未关联',
needCountO: 109 - res.Count,
needCountT: res.Count,
text: `总数`
})
})
},
}
watch: {
// 监听