기술나눔

uin-app WeChat 애플릿은 tabBar 하단 메뉴 구현을 사용자 정의합니다. 간단한 예(작업 메모)

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

WeChat 미니 프로그램에서 사용자 정의 구현 tabBar 이는 귀하의 애플리케이션에 보다 유연하고 개인화된 하단 탐색 메뉴를 제공할 수 있습니다.공식 WeChat 미니 프로그램으로 인해tabBar 제한된 구성 기능, 사용자 정의tabBar 복잡한 하단 탐색을 구현하는 것은 많은 개발자의 선택이 되었습니다.다음은 미니 프로그램에서 사용자 정의를 구현하는 방법에 대한 간단한 예입니다.tabBar

1단계: app.json 구성

먼저, 다음을 수행해야 합니다. app.json 중간 구성tabBar 기본적인 정보이지만custom 필드를 다음으로 설정해야 합니다.true 사용자 정의를 활성화하려면tabBar

{
  "pages": [
    "pages/index/index",
    "pages/logs/logs",
    "pages/profile/profile"
  ],
  "window": {
    "backgroundTextStyle": "light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "WeChat",
    "navigationBarTextStyle": "black"
  },
  "tabBar": {
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "首页"
      },
      {
        "pagePath": "pages/logs/logs",
        "text": "日志"
      },
      {
        "pagePath": "pages/profile/profile",
        "text": "我的"
      }
    ],
    "custom": true,
    "color": "#7A7E83",
    "selectedColor": "#3cc51f",
    "borderStyle": "black"
  }
}
  • 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

2단계: 사용자 정의 tabBar 구성 요소 만들기

라는 이름의 파일을 생성합니다. custom-tab-bar 폴더에 필요한 파일을 생성합니다(예:index.jsindex.jsonindex.wxmlindex.wxss)。

인덱스.js

현재 페이지를 기준으로 탭을 전환하는 등의 로직을 여기서 처리할 수 있습니다.

Component({
  data: {
    selected: 0,
    list: [{
      pagePath: '/pages/index/index',
      text: '首页'
    }, {
      pagePath: '/pages/logs/logs',
      text: '日志'
    }, {
      pagePath: '/pages/profile/profile',
      text: '我的'
    }]
  },
  methods: {
    switchTab: function(e) {
      const data = e.currentTarget.dataset;
      if (this.data.selected === data.index) return;
      wx.switchTab({url: data.path});
    },
    onShow: function(e) {
      // 当 tabBar 显示时触发,可以根据需要获取当前页面路径
      const { path } = getCurrentPages().pop();
      this.setData({
        selected: this.data.list.findIndex(tab => tab.pagePath === path)
      });
    }
  }
});
  • 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
인덱스.json

구성 요소의 구성 정보를 정의합니다.

{
  "usingComponents": {}
}
  • 1
  • 2
  • 3
인덱스.wxml

tabBar의 구조를 정의합니다.

<view class="tab-bar">
  <block wx:for="{{list}}" wx:key="index" wx:for-index="idx">
    <view class="tab-item {{selected === idx ? 'active' : ''}}" data-path="{{item.pagePath}}" data-index="{{idx}}" bindtap="switchTab">
      {{item.text}}
    </view>
  </block>
</view>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
인덱스.wxss

스타일을 추가하세요.

.tab-bar {
  display: flex;
  justify-content: space-around;
  background-color: #fff;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50px;
}

.tab-item {
  padding: 10px 0;
  text-align: center;
}

.tab-item.active {
  color: #3cc51f;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

3단계: app.js에서 맞춤 tabBar 사용

존재하다 app.js 사용자 정의 tabBar 구성 요소를 도입하고 사용하십시오.

App({
  onLaunch: function () {
    // 在这里可以执行一些启动时的逻辑
  },
  usingComponents: {
    '不同角色,控制查看底部菜单权限
    实现效果1:实体店![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/2ca6098eefaf4d55935d9eb8d44116fb.png)
实现效果2:回收公司
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/da1d7b225c064d3ab058176211f65833.png)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10