瀏覽代碼

导航栏-返回上一页

Xiao_123 1 月之前
父節點
當前提交
b2fac4b659
共有 1 個文件被更改,包括 33 次插入5 次删除
  1. 33 5
      components/Navbar/index.vue

+ 33 - 5
components/Navbar/index.vue

@@ -15,7 +15,7 @@
         :style="{'height': defaultLogoHeight + 'px'}"
       ></image>
       <view v-else class="nav-box-title" :style="{'height': height ? (height - statusBarHeight) + 'px' : '100%'}">
-        <uni-icons v-if="!textLeft" type="left" size="25" class="ss-m-l-20" color="#0E100F" @tap="goBack"></uni-icons>
+        <uni-icons v-if="!textLeft" :type="pages && pages.length > 1 ? 'left' : 'home'" size="23" class="ss-m-l-20" color="#0E100F" @tap="goBack"></uni-icons>
         <view class="title-text" :class="[{'text-left': textLeft}, {'text-center': !textLeft}]">{{ title }}</view>
       </view>
     </view>
@@ -52,7 +52,9 @@ const navbarHeight = ref(0)
 const statusBarHeight = ref(0)
 const height = ref(0)
 
+const pages = ref([])
 onLoad(async () => {
+  pages.value = getCurrentPages() // 获取当前页面栈的实例数组
   try {
     const systemInfo = uni.getSystemInfoSync()
     statusBarHeight.value = systemInfo.statusBarHeight
@@ -80,11 +82,37 @@ onLoad(async () => {
   }
 })
 
-// 返回上一页
+// 返回上一页或首页
 const goBack = () => {
-  uni.navigateBack({
-    delta: 1
-  })
+  if (pages.value.length > 1) {
+    uni.navigateBack({
+      delta: 1,
+      fail: () => {
+        console.error('返回上一页失败,尝试跳转到首页')
+        uni.reLaunch({
+          url: '/pages/index/position',
+          fail: (err) => {
+            console.error('跳转到首页也失败:', err)
+            uni.showToast({
+              title: '导航失败,请稍后重试',
+              icon: 'none'
+            })
+          }
+        })
+      }
+    })
+  } else {
+    uni.reLaunch({
+      url: '/pages/index/position',
+      fail: (err) => {
+        console.error('重新加载首页失败:', err)
+        uni.showToast({
+          title: '重新加载首页失败',
+          icon: 'none'
+        })
+      }
+    })
+  }
 }
 </script>