场景
需要获取数组的最后一个,结果获取时,数组还没有更新到最新,导致只能获取到倒数第二个
处理办法
在方法内使用 return new Promise ,并在调用方法后 .then ,即可实现方法执行后在执行
/** 查询项目单体列表 */
getList() {
this.loading = true;
const projectId = this.$store.state.project.currentProject.projectId;
//1.对方法进行改造
return new Promise((resolve, reject) => {
allBuild(projectId).then(response => {
this.buildList = response.data;
resolve(this.buildList) //2. 执行成功返回
}).catch(err => {
console.error(err);
}).finally(() => {
this.loading = false;
});
})
},
addBuild(this.form).then(response => {
this.msgSuccess("新增成功");
this.open = false;
this.isDisabled = false;
// 3. 需要让列表获取后,在执行操作
this.getList().then(list => {
//4.执行要处理的逻辑:滚动到最新一个
var buildId = this.buildList[this.buildList.length - 1].buildId;
this.scrollToView(`#build_${buildId}`);
});
}).catch(() => {
this.isDisabled = false;
});