refactor(top-nav-bar): add custom back prop

- add customBack prop to enable explicit custom back handling
- rewrite back navigation logic to use the new prop instead of inferring from emit listeners
- remove unused getCurrentInstance import
- update three AIGC page components to use the custom-back prop
This commit is contained in:
duanshuwen
2026-07-20 19:12:11 +08:00
parent c7062e0b51
commit 54a83bad5d
4 changed files with 9 additions and 22 deletions

View File

@@ -31,7 +31,7 @@
</template> </template>
<script setup> <script setup>
import { computed, getCurrentInstance, onMounted, ref } from "vue"; import { computed, onMounted, ref } from "vue";
// 定义props // 定义props
const props = defineProps({ const props = defineProps({
@@ -85,11 +85,14 @@ const props = defineProps({
type: Boolean, type: Boolean,
default: false, default: false,
}, },
customBack: {
type: Boolean,
default: false,
},
}); });
// 定义emits // 定义emits
const emit = defineEmits(["back"]); const emit = defineEmits(["back"]);
const instance = getCurrentInstance();
// 系统信息 // 系统信息
const statusBarHeight = ref(0); const statusBarHeight = ref(0);
@@ -145,25 +148,9 @@ const navContentStyle = computed(() => {
}; };
}); });
const hasBackListener = computed(() => {
const vnodeProps = instance?.vnode?.props || {};
return Boolean(vnodeProps.onBack);
});
// 处理返回事件 // 处理返回事件
const handleBack = () => { const handleBack = () => {
const pages = getCurrentPages(); if (props.customBack) {
if (pages.length <= 1) {
// #ifdef MP-WEIXIN
if (typeof wx !== "undefined" && typeof wx.exitMiniProgram === "function") {
wx.exitMiniProgram();
return;
}
// #endif
}
if (hasBackListener.value) {
emit("back"); emit("back");
return; return;
} }

View File

@@ -3,7 +3,7 @@
safe-area-inset-bottom @query="queryList"> safe-area-inset-bottom @query="queryList">
<template #top> <template #top>
<TopNavBar title="积分明细" background="transparent" title-color="#172033" back-icon-color="#172033" <TopNavBar title="积分明细" background="transparent" title-color="#172033" back-icon-color="#172033"
:align-with-menu-button="true" :z-index="3" @back="handleBack" /> custom-back :align-with-menu-button="true" :z-index="3" @back="handleBack" />
</template> </template>
<view class="points-details-content"> <view class="points-details-content">

View File

@@ -4,7 +4,7 @@
<template #top> <template #top>
<view class="aigc-record-nav"> <view class="aigc-record-nav">
<TopNavBar title="最近任务" background="transparent" title-color="#162235" back-icon-color="#162235" <TopNavBar title="最近任务" background="transparent" title-color="#162235" back-icon-color="#162235"
:align-with-menu-button="true" :z-index="1" @back="handleBack" /> custom-back :align-with-menu-button="true" :z-index="1" @back="handleBack" />
</view> </view>
</template> </template>

View File

@@ -2,7 +2,7 @@
<view class="shared-work-page"> <view class="shared-work-page">
<view class="shared-work-nav"> <view class="shared-work-nav">
<TopNavBar :title="shareDetail.templateName || '旅行作品'" background="#fff" title-color="#173a5f" <TopNavBar :title="shareDetail.templateName || '旅行作品'" background="#fff" title-color="#173a5f"
back-icon-color="#173a5f" :align-with-menu-button="true" :z-index="3" @back="handleBack" /> back-icon-color="#173a5f" custom-back :align-with-menu-button="true" :z-index="3" @back="handleBack" />
</view> </view>
<view class="shared-work-scroll"> <view class="shared-work-scroll">