CustomNavBar.vue 2.54 KB
<template>
    <view class="custom-nav" :style="{ height: statusBarHeight + 'px' }">
        <!-- 状态栏占位(高度由系统状态栏决定) -->
        <view :style="{ height: statusBarHeight + 'px' }"></view>
        <!-- 导航内容区 -->
        <view class="nav-content">
            <!-- 返回按钮(可根据是否首页控制显示) -->
            <view class="nav-left" @click="onBack">
                <uni-icons v-if="showBack" type="left" size="24"></uni-icons>
            </view>
            <!-- 标题 -->
            <view class="nav-title" v-if="title">{{ title }}</view>
            <!-- 右侧操作区 -->
            <view class="nav-right">
                <slot name="right"></slot>  <!-- 插槽:自定义右侧内容 -->
            </view>
        </view>
    </view>
</template>
<script lang="ts">
import {Component, Prop, Vue} from 'vue-facing-decorator';
import {onLoad} from "@dcloudio/uni-app";

@Component({})
export default class CustomNavBar extends Vue {
    @Prop({type: Boolean, required: false, default: false})
    showBack: boolean;

    @Prop({type: String, required: false})
    title: string;

    @Prop({type: String, required: false})
    backUrl: string;

// 状态栏高度(微信小程序中由系统提供)
    @Prop({type: Number, required: false, default: 48})
    statusBarHeight: number;

    created(): void {
        onLoad((option) => {
            // const systemInfo = uni.getWindowInfo();
            // this.statusBarHeight = systemInfo.statusBarHeight;
        });
    }

    onBack() {
        if (this.backUrl) {
            if (this.backUrl.startsWith('/pages/tabbar/')) {
                uni.switchTab({url: this.backUrl});
            } else {
                uni.reLaunch({url: this.backUrl});
            }
        } else {
            uni.navigateBack({delta: 1});
        }

    }
}
</script>


<style scoped lang="scss">
.custom-nav {
    width: 100%;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 999; // 确保在页面内容上方
}

.nav-content {
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    height: 44px;
}

.nav-left {
    position: absolute;
    left: 2px;
    width: 80rpx;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.back-icon {
    font-size: 36rpx;
    color: #333;
}

.nav-title {
    font-size: 32rpx;
    color: #333;
    font-weight: 400;
}

.nav-right {
    position: absolute;
    right: 16rpx;
    height: 100%;
    display: flex;
    align-items: center;
}
</style>