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

141 lines
2.9 KiB
Vue

<template>
<view class="nav-wrap">
<view class="status_bar"></view>
<view class="nav-title">
<view class="left" @click="back">
<u-icon v-if="showBack" name="arrow-left"></u-icon>
<u-icon v-else name="home" @click="toHome"></u-icon>
</view>
<view class="title">{{title}}</view>
<view class="right">
<slot name="right"></slot>
</view>
</view>
</view>
</template>
<script>
export default {
props: {
title: String,
},
data() {
return {
showBack: true, // 默认显示返回按钮
pagesLength: 0
}
},
methods: {
checkPagesLength() {
// 获取当前页面栈
const pages = getCurrentPages();
this.pagesLength = pages.length;
this.showBack = pages.length > 1;
console.log('当前页面栈长度:', pages.length, '显示返回按钮:', this.showBack);
},
back() {
console.log("返回");
if (this.$listeners && this.$listeners.back) {
this.$emit('back');
return;
}
if (this.showBack) {
// 有上一页,正常返回
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: flex-start;
height:110rpx;
line-height:110rpx;
background: #FFF;
justify-content: space-between;
padding:0 20rpx;
.left {
width: 80rpx;
height: 80rpx;
display: flex;
align-items: center;
justify-content: center;
.u-icon {
font-size: 40rpx;
}
}
.title {
flex: 1;
text-align: center;
font-size: 32rpx;
font-weight: bold;
color: #333;
}
.right {
width: 80rpx;
}
}
.status_bar {
height: var(--status-bar-height);
width: 100%;
}
</style>