CustomNavBar.vue
2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<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>