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

效果图

1.在components文件夹中新建MyTabbar组件

2.组件代码

html 复制代码
<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.父组件

html 复制代码
<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文件

html 复制代码
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
相关推荐
新中地GIS开发老师1 小时前
Cesium 军事标绘入门:用 Cesium-Plot-JS 快速实现标绘功能
前端·javascript·arcgis·cesium·gis开发·地理信息科学
Superxpang1 小时前
前端性能优化
前端·javascript·vue.js·性能优化
左手吻左脸。1 小时前
解决el-select因为弹出层层级问题,不展示下拉选
javascript·vue.js·elementui
左手吻左脸。1 小时前
Element UI表格中根据数值动态设置字体颜色
vue.js·ui·elementui
李白的故乡1 小时前
el-tree-select名字
javascript·vue.js·ecmascript
Rysxt_1 小时前
Element Plus 入门教程:从零开始构建 Vue 3 界面
前端·javascript·vue.js
隐含1 小时前
对于el-table中自定义表头中添加el-popover会弹出两个的解决方案,分别针对固定列和非固定列来隐藏最后一个浮框。
前端·javascript·vue.js
你的人类朋友1 小时前
先用js快速开发,后续引入ts是否是一个好的实践?
前端·javascript·后端
知识分享小能手1 小时前
微信小程序入门学习教程,从入门到精通,微信小程序核心 API 详解与案例(13)
前端·javascript·学习·react.js·微信小程序·小程序·vue
子兮曰2 小时前
npm workspace 深度解析:与 pnpm workspace 和 Lerna 的全面对比
前端·javascript·npm