refactor(Home): 重构待办数据加载逻辑为可重试方法
将setTimeout替换为$nextTick,并提取数据加载逻辑到单独方法 增加重试机制确保子组件加载完成 添加错误处理和日志记录
This commit is contained in:
@@ -1126,47 +1126,59 @@ export default {
|
||||
this.avatar = store.getters.avatar;
|
||||
},
|
||||
mounted() {
|
||||
setTimeout(() => {
|
||||
const listDataTask = [];
|
||||
if (!isEmpty(this.$refs.childOne) && this.$refs.childOne.hasPrem) {
|
||||
listDataTask.push(...this.$refs.childOne.taskList);
|
||||
}
|
||||
if (!isEmpty(this.$refs.child2) && this.$refs.child2.hasPrem) {
|
||||
listDataTask.push(...this.$refs.child2.taskList);
|
||||
}
|
||||
if (!isEmpty(this.$refs.child3) && this.$refs.child3.hasPrem) {
|
||||
listDataTask.push(...this.$refs.child3.taskList);
|
||||
}
|
||||
if (!isEmpty(this.$refs.child4) && this.$refs.child4.hasPrem) {
|
||||
listDataTask.push(...this.$refs.child4.taskList);
|
||||
}
|
||||
if (!isEmpty(this.$refs.child5) && this.$refs.child5.hasPrem) {
|
||||
listDataTask.push(...this.$refs.child5.taskList);
|
||||
}
|
||||
if (!isEmpty(this.$refs.child6) && this.$refs.child6.hasPrem) {
|
||||
listDataTask.push(...this.$refs.child6.taskList);
|
||||
}
|
||||
if (!isEmpty(this.$refs.child7) && this.$refs.child7.hasPrem) {
|
||||
listDataTask.push(...this.$refs.child7.taskList);
|
||||
}
|
||||
if (!isEmpty(this.$refs.child8) && this.$refs.child8.hasPrem) {
|
||||
listDataTask.push(...this.$refs.child8.taskList);
|
||||
}
|
||||
if (!isEmpty(this.$refs.child9) && this.$refs.child9.hasPrem) {
|
||||
listDataTask.push(...this.$refs.child9.taskList);
|
||||
}
|
||||
if (!isEmpty(this.$refs.child10) && this.$refs.child10.hasPrem) {
|
||||
listDataTask.push(...this.$refs.child10.taskList);
|
||||
}
|
||||
if (!isEmpty(this.$refs.child11) && this.$refs.child11.hasPrem) {
|
||||
listDataTask.push(...this.$refs.child11.taskList);
|
||||
}
|
||||
|
||||
this.listData = listDataTask;
|
||||
this.listData.sort((a, b) => b.value - a.value);
|
||||
}, 500);
|
||||
this.$nextTick(() => {
|
||||
this.loadTaskData();
|
||||
});
|
||||
},
|
||||
methods: {
|
||||
// 加载代办数据的方法
|
||||
loadTaskData(retryCount = 0) {
|
||||
const maxRetries = 10; // 最大重试次数
|
||||
const retryDelay = 100; // 重试间隔(ms)
|
||||
|
||||
try {
|
||||
const listDataTask = [];
|
||||
let allComponentsReady = true;
|
||||
|
||||
// 检查所有子组件是否已准备就绪
|
||||
const childRefs = [
|
||||
'childOne', 'child2', 'child3', 'child4', 'child5', 'child6',
|
||||
'child7', 'child8', 'child9', 'child10', 'child11'
|
||||
];
|
||||
|
||||
for (const refName of childRefs) {
|
||||
const childRef = this.$refs[refName];
|
||||
if (!isEmpty(childRef)) {
|
||||
if (childRef.hasPrem && childRef.taskList) {
|
||||
listDataTask.push(...childRef.taskList);
|
||||
}
|
||||
} else {
|
||||
// 如果某个组件还未准备好,标记为未就绪
|
||||
allComponentsReady = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (allComponentsReady || retryCount >= maxRetries) {
|
||||
// 所有组件都准备好了,或者已达到最大重试次数
|
||||
this.listData = listDataTask;
|
||||
this.listData.sort((a, b) => b.value - a.value);
|
||||
console.log('代办数据加载完成,共', this.listData.length, '条数据');
|
||||
} else {
|
||||
// 还有组件未准备好,延迟重试
|
||||
setTimeout(() => {
|
||||
this.loadTaskData(retryCount + 1);
|
||||
}, retryDelay);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('加载代办数据时出错:', error);
|
||||
// 出错时也尝试重试
|
||||
if (retryCount < maxRetries) {
|
||||
setTimeout(() => {
|
||||
this.loadTaskData(retryCount + 1);
|
||||
}, retryDelay);
|
||||
}
|
||||
}
|
||||
},
|
||||
going(url) {
|
||||
this.$router.push(url);
|
||||
},
|
||||
|
Reference in New Issue
Block a user