taro底部导航,Tabbar

没有特别的幸运,那么就特别的努力!!!

配置信息

官方给出:

在 app.config 中按正常填写 tabBar 项的相关配置(为了向下兼容),并把 tabBar 项的 custom 字段设置为 true。但我试过 custom设置为 false 才生效

1.app.config.js 文件配置

js 复制代码
// app.config.js
export default defineAppConfig({
  pages: ["pages/index/index", "pages/complaint/index", "pages/mine/index"],

  window: {
    backgroundTextStyle: "light",
    navigationBarBackgroundColor: "#fff",
    navigationBarTitleText: "WeChat",
    navigationBarTextStyle: "black",
  },
  tabBar: {
    custom: false,
    color: "#000000",
    selectedColor: "#DC143C",
    backgroundColor: "#ffffff",
    list: [
      {
        pagePath: "pages/index/index",
        selectedIconPath: "images/tabbar_home_on.png",
        iconPath: "images/tabbar_home.png",
        text: "首页",
      },
      {
        pagePath: "pages/mine/index",
        selectedIconPath: "images/tabbar_my_on.png",
        iconPath: "images/tabbar_my.png",
        text: "个人中心",
      },
    ],
  },
});

2.添加 custom-tab-bar

在 src 目录下添加 custom-tab-bar 目录,在里面书写组件,支持 React、Vue 和原生写法。

js 复制代码
├── config
├── src
|   └── custom-tab-bar
|       ├── index.config.ts
|       └── index.tsx
└── package.json
js 复制代码
// src/custom-tab-bar/index.tsx
import { Component } from 'react'
import Taro from '@tarojs/taro'
import { CoverView, CoverImage } from '@tarojs/components'

import './index.scss'

export default class Index extends Component {
  state = {
    selected: 0,
    color: '#000000',
    selectedColor: '#DC143C',
    list: [
      {
        pagePath: '/pages/index/index',
        selectedIconPath: '../images/tabbar_home_on.png',
        iconPath: '../images/tabbar_home.png',
        text: '首页'
      },
      {
        pagePath: '/pages/mine/index',
        selectedIconPath: '../images/tabbar_my_on.png',
        iconPath: '../images/tabbar_my.png',
        text: '个人中心'
      }
    ]
  }

  switchTab(index, url) {
    this.setSelected(index)
    Taro.switchTab({ url })
  }

  setSelected (idx: number) {
    this.setState({
      selected: idx
    })
  }

  render() {
    const { list, selected, color, selectedColor } = this.state

    return (
      <CoverView className='tab-bar'>
        <CoverView className='tab-bar-border'></CoverView>
        {list.map((item, index) => {
          return (
            <CoverView key={index} className='tab-bar-item' onClick={this.switchTab.bind(this, index, item.pagePath)}>
              <CoverImage src={selected === index ? item.selectedIconPath : item.iconPath} />
              <CoverView style={{ color: selected === index ? selectedColor : color }}>{item.text}</CoverView>
            </CoverView>
          )
        })}
      </CoverView>
    )
  }
}

3.底部组件都需要引入

js 复制代码
// src/page/mine/index.tsx
import { View, Text } from '@tarojs/components'
import Taro,{ useLoad } from '@tarojs/taro'
import './index.scss'

import type CustomTabBar from '../../custom-tab-bar'

export default function Mine () {
  const pageCtx = Taro.getCurrentInstance().page

  useLoad(() => {
    const tabbar = Taro.getTabBar<CustomTabBar>(pageCtx)
    tabbar?.setSelected(1)
  })

  return (
    <View className='mine'>
      <Text>mine-1</Text>
    </View>
  )
}

希望能帮助到大家,同时祝愿大家在开发旅途中愉快!!!

拿着 不谢 请叫我"锤" !!!

相关推荐
锋行天下2 小时前
公司内网部署大模型的探索之路
前端·人工智能·后端
1024肥宅2 小时前
手写 EventEmitter:深入理解发布订阅模式
前端·javascript·eventbus
海市公约4 小时前
HTML网页开发从入门到精通:从标签到表单的完整指南
前端·ide·vscode·程序人生·架构·前端框架·html
purpleseashell_Lili4 小时前
如何学习 AG-UI 和 CopilotKit
javascript·typescript·react
行云流水6264 小时前
前端树形结构实现勾选,半勾选,取消勾选。
前端·算法
diudiu_334 小时前
web漏洞--认证缺陷
java·前端·网络
阿珊和她的猫5 小时前
<video>` 和 `<audio>` 标签的常用属性解析
前端
LSL666_5 小时前
4 jQuery、JavaScript 作用域、闭包与 DOM 事件绑定
前端·javascript·html
yinuo5 小时前
前端跨页面通讯终极指南⑤:window.name 用法全解析
前端
小飞侠在吗5 小时前
vue computed 和 watch
前端·javascript·vue.js