WMS_PDA/components/page-nav/page-nav.vue

168 lines
3.5 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<view class="nav-wrap">
<view class="status_bar"></view>
<view class="nav-title">
<view class="left">
<view class="left-back" @click="back">
<u-icon v-if="effectiveShowBack" name="arrow-left"></u-icon>
<u-icon v-else name="home" @click="toHome"></u-icon>
</view>
<slot name="left"></slot>
</view>
<view class="title">{{title}}</view>
<view class="right">
<slot name="right"></slot>
</view>
</view>
</view>
</template>
<script>
export default {
props: {
title: String,
showBack: {
type: Boolean,
default: undefined
}
},
data() {
return {
internalShowBack: true, // 默认显示返回按钮
pagesLength: 0
}
},
computed: {
effectiveShowBack() {
// 如果父组件显式传了 showBack,优先使用;否则用自动检测值
return this.showBack !== undefined ? this.showBack : this.internalShowBack;
}
},
methods: {
checkPagesLength() {
// 获取当前页面栈
const pages = getCurrentPages();
this.pagesLength = pages.length;
this.internalShowBack = pages.length > 1;
console.log('当前页面栈长度:', pages.length, '显示返回按钮:', this.internalShowBack);
},
back() {
console.log("返回");
if (this.$listeners && this.$listeners.back) {
this.$emit('back');
return;
}
if (this.effectiveShowBack) {
// 有上一页,正常返回
uni.navigateBack({
delta: 1,
fail: (err) => {
console.log('返回失败:', err);
// 返回失败时也跳转到首页
this.toHome();
}
});
} else {
// 没有上一页,跳转到首页
this.toHome();
}
},
toHome() {
console.log('跳转到首页');
const pages = getCurrentPages();
const currentRoute = pages.length ? `/${pages[pages.length - 1].route || ''}` : '';
if (currentRoute === '/pages/index/index') {
console.log('当前已在首页,取消重复跳转');
return;
}
// 根据你的路由配置调整首页路径
// 假设首页是 tabBar 页面
uni.switchTab({
url: '/pages/index/index', // 替换为你的首页路径
fail: (err) => {
console.error('跳转首页失败:', err);
// 如果 switchTab 失败,尝试 reLaunch
uni.reLaunch({
url: '/pages/index/index'
});
}
});
}
},
created() {
// 组件创建时检查
this.checkPagesLength();
},
mounted() {
// 挂载时再次检查
this.$nextTick(() => {
this.checkPagesLength();
});
}
}
</script>
<style lang="scss" scoped>
.nav-wrap {
position: relative;
}
.nav-title {
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
flex-direction: row;
align-items: center;
justify-content: space-between;
height:110rpx;
line-height:110rpx;
background: #FFF;
padding:0 20rpx;
position: relative;
.left {
min-width: 80rpx;
height: 80rpx;
display: flex;
align-items: center;
justify-content: flex-start;
gap: 10rpx;
.left-back {
width: 80rpx;
height: 80rpx;
display: flex;
align-items: center;
justify-content: center;
}
.u-icon {
font-size: 40rpx;
}
}
.title {
position: absolute;
left: 50%;
transform: translateX(-50%);
white-space: nowrap;
font-size: 32rpx;
font-weight: bold;
color: #333;
}
.right {
width: 80rpx;
display: flex;
align-items: center;
justify-content: flex-end;
}
}
.status_bar {
height: var(--status-bar-height);
width: 100%;
}
</style>