uniapp + uviewui —— u-tabbar的使用

文章介绍了如何在uni-app中启用自定义tabBar并在Vue组件中使用Vuex进行状态管理,以实现底部导航的动态渲染和点击高亮效果。通过pages.json设置custom为true,结合Vuex的state、getters、mutations和actions进行数据绑定和更新。
摘要由CSDN通过智能技术生成
注: 每个底部导航页面里都要有该代码才能生效
1. pages.json 生效代码 “custom”: true
"tabBar": {
		"custom": true,
		"list": [
			{
				"pagePath": "pages/index/index"
			},
			{
				"pagePath": "pages/my/index"
			}
		]
	}
2.使用代码管理动态渲染绑定值
state: {
	pageValue: 0,
	pageList: [
		{
			path: 'pages/index/index',
			text: '首页',
			icon: 'home'
		},
		{
			path: 'pages/my/index',
			text: '我的',
			icon: 'account'
		},
	]
},
getters: {
	pageList: state => state.pageList,
	pageValue: state => state.pageValue
},
mutations: {
	SET_PAGE(state, data) {
		state.pageValue = data
	}
},
actions: {}
3. 页面具体使用及完善
<u-tabbar
      :value="pageValue"
      :fixed="true"
      :border="false"
      :placeholder="true"
      :safeAreaInsetBottom="true"
    >
      <u-tabbar-item
        v-for="item in pageList"
        :key="item.path"
        :icon="item.icon"
        :text="item.text"
        @click="clickPage(item)"
      />
  </u-tabbar>
import { mapGetters, mapMutations } from 'vuex'
computed: {
    ...mapGetters(['pageValue']),
    ...mapGetters(['pageList'])
  },
methods: {
	...mapMutations(['SET_PAGE']), // 注意参数必须是数组
	clickPage(item) {
		this.SET_PAGE(index) // 这样写底部导航高亮才是正确的
		uni.navigateTo({
			url: item.path
		})
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值