当前位置: 首页 > news >正文

taro底部导航,Tabbar

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

配置信息

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

1.app.config.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 和原生写法。

├── config
├── src
|   └── custom-tab-bar
|       ├── index.config.ts
|       └── index.tsx
└── package.json
// 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.statereturn (<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.底部组件都需要引入

// 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().pageuseLoad(() => {const tabbar = Taro.getTabBar<CustomTabBar>(pageCtx)tabbar?.setSelected(1)})return (<View className='mine'><Text>mine-1</Text></View>)
}

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

拿着 不谢 请叫我“锤” !!!


http://www.mrgr.cn/news/54122.html

相关文章:

  • MyBatis入门程序之客户添加、更新与删除
  • 重置时把el-tree树节点选中状态取消
  • Android 内存优化——常见内存泄露及优化方案
  • python绝对值怎么表示
  • stable diffusion【win+Mac版】超详细安装教程(附stable diffusion 整合包)
  • RHCE——时间服务器
  • 电能表预付费系统-标准传输规范(STS)(13)
  • 【str_replace替换导致的绕过】
  • 解决因内存过小芯片使用malloc造成内存碎片使程序偶发性卡死问题
  • mysql 10 单表访问方法
  • Java 数据基本类型详解(各基本数据类型及其大小、数据类型转换、数据溢出问题、自动装箱与拆箱的影响)
  • 架构师之路-学渣到学霸历程-23
  • 理解C#中空值条件运算符及空值检查简化
  • 十五、Python基础语法(list(列表)-上)
  • AI写作助手系统盈利模式分析:打造盈利的AI网站
  • 可能要招1000+应届生!直击美团心动岗位 - 美团面试原题 - 贪心算法题如何用 go 和 C++ 解决
  • 【CSAPP】【答案/解析】《深入理解计算机系统》实验一/datalab-handout实验
  • 记录迷茫!
  • 【运维基础知识】《Linux 系统架构与文件系统及权限管理全解析》
  • java反射介绍
  • Kubernetes运行 Llama3
  • ntfs MFT损坏(ntfs文件系统故障)导致oracle异常恢复---惜分飞
  • 使用Version Catalog在项目之间共享版本
  • target_include_directories是如何组织头文件的?
  • Android开发相关的重要网站
  • 《数字图像处理基础》学习02-BMP位图文件