From 0fddfbfa74ac48c463faa32085893966c7d35c5d Mon Sep 17 00:00:00 2001 From: ningbo <3301955438@qq.com> Date: Sat, 16 Aug 2025 20:32:00 +0800 Subject: [PATCH] =?UTF-8?q?refactor(Home):=20=E9=87=8D=E6=9E=84=E5=BE=85?= =?UTF-8?q?=E5=8A=9E=E6=95=B0=E6=8D=AE=E5=8A=A0=E8=BD=BD=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E4=B8=BA=E5=8F=AF=E9=87=8D=E8=AF=95=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将setTimeout替换为$nextTick,并提取数据加载逻辑到单独方法 增加重试机制确保子组件加载完成 添加错误处理和日志记录 --- src/views/Home/index-new-blue.vue | 90 +++++++++++++++++-------------- 1 file changed, 51 insertions(+), 39 deletions(-) diff --git a/src/views/Home/index-new-blue.vue b/src/views/Home/index-new-blue.vue index 9dff15b..af319ee 100644 --- a/src/views/Home/index-new-blue.vue +++ b/src/views/Home/index-new-blue.vue @@ -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); },