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

news/2024/10/24 23:21:07/

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

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.ppmy.cn/news/1207857.html

相关文章

两台linux虚拟机之间实现免密登录

主要实现两台虚拟机之间的免密登录&#xff0c;总所周知&#xff0c;虚拟机之间登录使用的协议是ssh协议&#xff0c;端口号是 22 主机 创建对应的加密文件 [rootweb-2 ~]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.s…

深入理解JVM虚拟机第二十一篇:详解JVM当中的操作数在栈以及分析操作数栈与字节码指令和执行引擎的关系图解

大神链接:作者有幸结识技术大神孙哥为好友,获益匪浅。现在把孙哥视频分享给大家。 孙哥链接:孙哥个人主页 作者简介:一个颜值99分,只比孙哥差一点的程序员 本专栏简介:话不多说,让我们一起干翻JVM 本文章简介:话不多说,让我们讲清楚JVM当中的操作数在栈以及分析操作数…

如何在3dMax中使用MaxScript在视口中显示数据?

如何在3dMax中使用MaxScript在视口中显示数据&#xff1f; 详细的教程指南&#xff0c;介绍如何使用MaxScript在视口中直接显示对象名称、坐标和顶点索引等信息。 在本教程中&#xff0c;您将学习如何借助MaxScript在视口中直接显示对象信息或数据。 本教程介绍了如何显示简单的…

看李广的故事:发现团队管理之道

在漠北之战中&#xff0c;李广因迷失道路而延误了军期。因李广年事已高&#xff0c;无法承受幕府的责难&#xff0c;最终选择在军前自刎而死。 这一事件令人痛惜&#xff0c;不禁让人想起在工作中遇到的类似情况。有些同事因为突然离职&#xff0c;让领导感到愕然&#xff0c;…

SRRC认证的必要性:保障电子产品质量安全的重要措施

随着电子产品的普及和应用&#xff0c;对电子产品的质量安全要求也越来越高。为了保障消费者的权益和安全&#xff0c;国家对电子产品进行了严格的监管和管理。其中&#xff0c;SRRC认证是保障电子产品质量安全的重要措施之一。 SRRC认证是指在我国境内生产、销售、使用的无线电…

Kotlin与Java写法的变更

目录 获取类的Java Class属性 类型检查 for循环 switch语句 if判断 获取类的Java Class属性 //Java Intent intent new Intent(this, MainActivity.class);//Kotlin val intent Intent(this, MainActivity::class.java) 类型检查 //Java apple instanceof Fruit !(app…

【Unity实战】实现强大通用易扩展的对话系统(附项目源码)

先看看实现的最终效果 前言 之前的对话系统因为存在一些错误和原作者不允许我分享&#xff0c;所以被我下架了&#xff0c;而且之前对话系统确实少了一些功能&#xff0c;比如最基本的逐字打印功能&#xff0c;原本来是打算后面补充的。 对话系统在游戏中实现太常见了&#x…

mysql 索引执行过程 回表

MySQL索引执行过程如下&#xff1a; MySQL首先通过B树索引快速定位到符合条件的行&#xff1b;MySQL通过回表&#xff08;也称为聚簇索引&#xff09;找到精确的数据&#xff1b;如果回表的代价过高&#xff0c;MySQL会考虑使用覆盖索引&#xff0c;即只从索引树中获取所需的所…