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

uniapp+uview2.0+vuex实现自定义tabbar组件

效果图
在这里插入图片描述

1.在components文件夹中新建MyTabbar组件
在这里插入图片描述
2.组件代码

<template><view class="myTabbarBox" :style="{ backgroundColor: backgroundColor }"><u-tabbar :placeholder="true" zIndex="0" :value="MyTabbarState" @change="tabbarChange" :fixed="false":activeColor="tabbarSet.activeColor" :inactiveColor="tabbarSet.inactiveColor" :safeAreaInsetBottom="true"><u-tabbar-item v-for="(item,index) in tabbarSet.list" :text="item.title"><image class="u-page__item__slot-icon" slot="active-icon" :src="item.image[1]"></image><image class="u-page__item__slot-icon" slot="inactive-icon" :src="item.image[0]"></image></u-tabbar-item></u-tabbar></view>
</template><script>export default {data() {return {backgroundColor: "#fff",// MyTabbarState: this.$store.getters.MyTabbarState,tabbarSet: this.$store.state.tabbarSet,//这里用到了vuex存储数据};},computed: {MyTabbarState() {return this.$store.getters.MyTabbarState;},},// watch: {// 	MyTabbarState: {// 		handler(newVal, oldVal) {// 			// console.log('更新', newVal, oldVal)// 		},// 		deep: true, // 深度监听// 		immediate: true, //立即执行// 	}// },methods: {//选项切换时tabbarChange(e) {console.log('change1', e, this.tabbarSet, this.MyTabbarState);this.MyTabbarState = e;this.$store.dispatch('getMyTabbarState', e)uni.navigateTo({url: this.tabbarSet.list[e].url})}},}
</script><style lang="scss">.u-page__item__slot-icon {width: 41rpx;height: 44rpx;}.myTabbarBox {position: fixed;bottom: 0;left: 0;z-index: 999999999;width: 100%;padding: 30rpx 0;}::v-deep.u-tabbar__content {background-color: transparent;}
</style>

3.父组件

<template><view><!-- 底部tabbar --><MyTabbar></MyTabbar></view></template><script>export default {data() {return {}},mounted() {let MyTabbarState = 0;let tabbarSet = {backgroundColor: "#fff", //背景颜色activeColor: "#000", //点击后的字体颜色inactiveColor: "#D0D0D0", //默认字体颜色list: [{title: "首页",image: ["../../static/previousHome.png", "../../static/backHome.png"],url: "/pages/index/index"},{title: "我的",image: ["../../static/previousUser.png", "../../static/backUser.png"],url: "/pages/order/order"}]};this.$store.dispatch('getTabbarSet', tabbarSet);this.$store.dispatch('getMyTabbarState', MyTabbarState);},onShow() {//改变底部导航栏状态this.$store.commit('get_MyTabbarState', 0);}}

4.创建store目录,下面创建index.js文件

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const debug = process.env.NODE_ENV !== 'production'const loginKey = 'login_status'const vuexStore = new Vuex.Store({state: {isBtnShow:false,//按钮节流MyTabbarState:1,//操作栏选中状态tabbarSet:{},	// 操作栏数据},mutations: {// 操作栏数据get_tabbarSet(state, goName){console.log('MUTATION',goName)state.tabbarSet = goName;cookie.set('tabbarSet', goName)},//操作栏选中状态get_MyTabbarState(state, goName){console.log('改变状态',goName)state.MyTabbarState = goName;cookie.set('MyTabbarState', goName)}},actions: {//操作栏选中状态getMyTabbarState({ state, commit }, force) {commit('get_MyTabbarState',force)},// 操作栏数据getTabbarSet({ state, commit }, force) {commit('get_tabbarSet',force)},changeIsBtnshow({ state, commit }, index) {commit('updateIsBtnShow', index)},},getters: {MyTabbarState:state=>state.MyTabbarState,tabbarSet:state => state.tabbarSet,isBtnShow:state => state.isBtnShow},strict: debug,
})export default vuexStore
http://www.lryc.cn/news/224588.html

相关文章:

  • opencv 任意两点切割图像
  • rust变量绑定、拷贝、转移、引用
  • Java多种方式向图片添加自定义水印、图片转换及webp图片压缩
  • 基于Pytorch框架的LSTM算法(二)——多维度单步预测
  • cnn感受野计算方法
  • 百分点科技受邀参加“第五届治理现代化论坛”
  • 基于Springboot的智慧食堂设计与实现(有报告)。Javaee项目,springboot项目。
  • 「Verilog学习笔记」多功能数据处理器
  • OpenHarmony 4.0 Release 编译异常处理
  • 软件测试|MySQL LIKE:深入了解模糊查询
  • linux防火墙设置
  • http 403
  • RAW图像处理软件Capture One 23 Enterprise mac中文版功能特点
  • Linux 进程终止和等待
  • python用tkinter随机数猜数字大小
  • 程序员们保住自己饭碗
  • 顶板事故防治vr实景交互体验提高操作人员安全防护技能水平
  • 为什么推荐从Linux开始了解IT技术
  • 【Mysql】增删改查(基础版)
  • 文件夹找不到了怎么恢复?4个正确恢复方法分享!
  • 迅为RK3568开发板GPS模块测试实验步骤
  • 用趋动云GPU部署自己的Stable Diffusion
  • nfs配置
  • 说话人识别声纹识别CAM++,ECAPA-TDNN等算法
  • 某平台简单尝试一次密码逆向
  • 微信号绑定50个开发者小程序以后超额如何删除不用的
  • 【Cheat Engine7.5】基础教程第三关(步骤4)
  • 141. 环形链表 --力扣 --JAVA
  • 电子元器件的结温壳温与环境温度经验总结
  • Spring Gateway基础知识总结