| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <template>
- <!-- 底部导航栏 -->
- <view class="bottom-tabbar">
- <view class="tabbar-container">
- <view
- v-for="(item,index) in list" :key="index"
- class="tab-item"
- :class="{ 'active':currentTab === item.pathText }"
- @click="switchTab(item.pathText)"
- >
- <image
- class="tab-icon"
- :src="item.pathText === currentTab ?item.oneIcon : item.twoIcon"
- mode="aspectFit"
- ></image>
- <text class="tab-text" :class="{ 'active-text': currentTab === item.pathText }">{{item.name}}</text>
- </view>
- </view>
- </view>
- </template>
- <script>
- export default {
- name: 'BarNavigasiHandap',
- data() {
- return {
- currentTab: 'index' ,// 当前选中的tab
- list:[
- {
- name:'首页',
- pathText:'index',
- oneIcon:'/static/images/tabbar/home_.png',
- twoIcon:'/static/images/tabbar/home.png',
-
- },
- {
- name:'我的',
- pathText:'mine',
- oneIcon:'/static/images/tabbar/mine_.png',
- twoIcon:'/static/images/tabbar/mine.png',
-
- }
- ]
- }
- },
- mounted() {
- // 组件挂载时设置当前tab
- this.setCurrentTab()
- // 监听页面显示事件
- uni.$on('pageShow', () => {
- this.setCurrentTab()
- })
- },
- beforeDestroy() {
- // 组件销毁前移除监听
- uni.$off('pageShow')
- },
- methods: {
- // 设置当前tab
- setCurrentTab() {
- try {
- const pages = getCurrentPages()
- if (pages && pages.length > 0) {
- const currentPage = pages[pages.length - 1]
- const route = currentPage.route || ''
- console.log('当前路由:', route) // 调试用
- // 更精确的路由匹配
- if (route === 'pages/indexOrder/index') {
- this.currentTab = 'index'
- } else if (route === 'pages/mine/index') {
- this.currentTab = 'mine'
- } else if (route.includes('mine')) {
- // 如果路由包含 mine,也设置为 mine(处理子页面)
- this.currentTab = 'mine'
- } else if (route.includes('index') && !route.includes('mine')) {
- // 如果路由包含 index 但不包含 mine,设置为 index
- this.currentTab = 'index'
- }
- }
- } catch (e) {
- console.error('获取当前页面失败:', e)
- }
- },
- // 切换tab
- switchTab(tab) {
- // 如果点击的是当前tab,不执行操作
- if (this.currentTab === tab) {
- return
- }
-
- // 先更新状态,立即高亮(关键:在跳转前更新状态)
- this.currentTab = tab
-
- // 跳转到对应页面(使用 reLaunch 关闭所有页面并跳转)
- const urlMap = {
- 'index': '/pages/indexOrder/index',
- 'mine': '/pages/mine/index'
- }
-
- const targetUrl = urlMap[tab]
- if (targetUrl) {
- uni.reLaunch({
- url: targetUrl,
- success: () => {
- // 跳转成功后再次确认状态
- setTimeout(() => {
- this.setCurrentTab()
- }, 100)
- },
- fail: (err) => {
- console.error('页面跳转失败:', err)
- // 跳转失败时恢复状态
- this.setCurrentTab()
- }
- })
- }
- }
- }
- }
- </script>
- <style scoped>
- .bottom-tabbar {
- position: fixed;
- bottom: 0;
- left: 0;
- right: 0;
- width: 100%;
- background-color: #F5F5F5;
- padding: 16rpx 24rpx;
- padding-bottom: calc(16rpx + env(safe-area-inset-bottom));
- z-index: 98;
- box-sizing: border-box;
- }
- .tabbar-container {
- display: flex;
- justify-content: space-evenly;
- align-items: center;
- width: 344rpx;
- margin: auto;
- height: 115rpx;
- background: #FFFFFF;
- box-shadow: 0rpx 8rpx 38rpx 0rpx rgba(40,86,199,0.2);
- border-radius: 57rpx 57rpx 57rpx 57rpx;
-
- }
- .tab-item {
- font-weight: 500;
- font-size: 23rpx;
- color: #AAAAAA;
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- min-height: 100rpx;
- }
- .tab-item.active {
- font-size: 23rpx;
- color: #323232;
- }
- .tab-icon {
- width: 49.62rpx;
- height: 49.62rpx;
- margin-bottom: 6rpx;
- }
- .tab-text {
- font-size: 26rpx;
- color: #999999;
- line-height: 1;
- }
- .tab-text.active-text {
- color: #323232;
- font-weight: 500;
- }
- </style>
|