Explorar el Código

fix(生命周期): 修复组件生命周期事件监听和清理逻辑

ylong hace 1 mes
padre
commit
5ded164fcf

+ 11 - 3
pages/index/audit/components/time-clock.vue

@@ -29,14 +29,13 @@ export default {
         }
     },
     computed: {
-        // 格式化时间:毫秒 → 00:00:00.00 格式(毫秒两位
+        // 格式化时间:00:00:00 格式(不显示毫秒)
         formattedTime() {
             const elapsed = this.baseMillis + (this.nowTs - this.startTs)
             const totalMs = Math.max(0, Math.floor(elapsed))
             const hours = String(Math.floor(totalMs / 3600000)).padStart(2, '0')
             const minutes = String(Math.floor((totalMs % 3600000) / 60000)).padStart(2, '0')
             const seconds = String(Math.floor((totalMs % 60000) / 1000)).padStart(2, '0')
-            // const ms2 = String(Math.floor((totalMs % 1000) / 10)).padStart(2, '0')
             return `${hours}:${minutes}:${seconds}`
         }
     },
@@ -84,7 +83,16 @@ export default {
     },
     // 组件卸载时清理定时器(避免内存泄漏,与 Vue3 一致)
     beforeDestroy() {
-        if (this._unsub) this._unsub()
+        if (this._unsub) {
+            this._unsub()
+            this._unsub = null
+        }
+    },
+    beforeUnmount() {
+        if (this._unsub) {
+            this._unsub()
+            this._unsub = null
+        }
     }
 }
 </script>

+ 9 - 2
pages/index/detail/batch-audit.vue

@@ -40,7 +40,7 @@
 
 <script setup>
 import { ref, onUnmounted } from "vue";
-import { onLoad, onShow } from "@dcloudio/uni-app";
+import { onLoad, onShow, onHide } from "@dcloudio/uni-app";
 import BookItem from "./components/BookItem2.vue";
 
 const detailMap = ref({});
@@ -198,7 +198,7 @@ const { unregister, register } = uni.$u.useEventListener((e) => {
     if (e.barcode) {
         handleScan(e.barcode);
     }
-},true);
+});
 // #endif
 
 onLoad((options) => {
@@ -219,6 +219,13 @@ onShow(() => {
     orderDetail.value = uni.getStorageSync("orderDetail");
     register();
 });
+
+onHide(() => {
+    // #ifdef APP-PLUS
+    unregister();
+    // #endif
+});
+
 onUnmounted(() => {
     // #ifdef APP-PLUS
     unregister();

+ 9 - 2
pages/index/detail/book-audit.vue

@@ -64,7 +64,7 @@
 
 <script setup>
 import { ref, nextTick, onUnmounted } from "vue";
-import { onLoad, onShow } from "@dcloudio/uni-app";
+import { onLoad, onShow, onHide } from "@dcloudio/uni-app";
 import AuditInfo from "./components/AuditInfo.vue";
 import FileInfo from "./components/FileInfo.vue";
 
@@ -215,7 +215,7 @@ onLoad((options) => {
 // #ifdef APP-PLUS
 const { unregister, register } = uni.$u.useEventListener((e) => {
     handleScan(e.barcode);
-},true);
+});
 // #endif
 
 
@@ -226,6 +226,13 @@ onShow(() => {
         orderDetail.value = uni.getStorageSync("orderDetail");
     }
 });
+
+onHide(() => {
+    // #ifdef APP-PLUS
+    unregister();
+    // #endif
+});
+
 onUnmounted(() => {
     // #ifdef APP-PLUS
     unregister();

+ 22 - 1
pages/index/detail/review-detail.vue

@@ -54,7 +54,7 @@
 
 <script setup>
 import { ref, onUnmounted } from "vue";
-import { onLoad, onShow } from "@dcloudio/uni-app";
+import { onLoad, onShow, onHide } from "@dcloudio/uni-app";
 import AuditorSelector from "./components/AuditorSelector.vue";
 import OrderInfo from "./components/OrderInfo.vue";
 import UserInfoCard from "./components/UserInfoCard.vue";
@@ -298,10 +298,31 @@ onLoad((options) => {
 });
 
 
+const { unregister, register } = uni.$u.useEventListener((e) => {
+    handleScan(e.barcode);
+});
+
 onShow(() => {
+    register();
     // 只有在首次加载时才调用getOrderDetail,避免重复请求
     getOrderDetail();
 });
+
+onHide(() => {
+    // #ifdef APP-PLUS
+    if (typeof unregister === 'function') {
+        unregister();
+    }
+    // #endif
+});
+
+onUnmounted(() => {
+    // #ifdef APP-PLUS
+    if (typeof unregister === 'function') {
+        unregister();
+    }
+    // #endif
+});
 </script>
 
 <style>