uniapp dynamic tabBar (the whole process includes vuex component configuration, etc.)

   (shipping authority)

 

 (Unloading authority)

1. First we need to configure tabbar and pages in pages.json (configure all tabbar paths)

"pages": [ //pages数组中第一项表示应用启动页,
		{
			"path": "pages/loadAndUnloadVessel/loadVessel/loadVesselPlan",
			"style": {
				"navigationBarTitleText": "装船作业计划",

				"app-plus": {
					"titleNView": false,
					"bounce": "none"
				}
			}
		}, {
			"path": "pages/loadAndUnloadVessel/loadVessel/loadVesselCommand",
			"style": {
				"navigationBarTitleText": "装船指令",
				"app-plus": {
					"titleNView": false,
					"bounce": "none"
				}
			}
		}, {
			"path": "pages/loadAndUnloadVessel/loadVessel/loadVesselHistory",
			"style": {
				"navigationBarTitleText": "历史",
				"app-plus": {
					"titleNView": false,
					"bounce": "none"
				}
			}
		}, {
			"path": "pages/loadAndUnloadVessel/unloadVessel/unloadVesselPlan",
			"style": {
				"navigationBarTitleText": "卸船作业计划",
				"app-plus": {
					"titleNView": false,
					"bounce": "none"
				}
			}
		}, {
			"path": "pages/loadAndUnloadVessel/unloadVessel/unloadVesselCommand",
			"style": {
				"navigationBarTitleText": "卸船指令",
				"app-plus": {
					"titleNView": false,
					"bounce": "none"
				}
			}
		}, {
			"path": "pages/loadAndUnloadVessel/unloadVessel/unloadVesselHistory",
			"style": {
				"navigationBarTitleText": "历史",
				"app-plus": {
					"titleNView": false,
					"bounce": "none"
				}
			}
		}, 

	],	
"tabBar": {
		"color": "#7a7e83",
		"selectedColor": "#0faeff",
		"backgroundColor": "#ffffff",
        // "custom": true,custom(自定义),不开启的话,在页面是有占位的,就需要在页面进行隐藏
		"list": [{
				"pagePath": "pages/loadAndUnloadVessel/loadVessel/loadVesselPlan"

			}, {
				"pagePath": "pages/loadAndUnloadVessel/loadVessel/loadVesselCommand"

			},
			{
				"pagePath": "pages/loadAndUnloadVessel/loadVessel/loadVesselHistory"

			},
			{
				"pagePath": "pages/loadAndUnloadVessel/unloadVessel/unloadVesselPlan"

			}, {
				"pagePath": "pages/loadAndUnloadVessel/unloadVessel/unloadVesselCommand"

			},
			{
				"pagePath": "pages/loadAndUnloadVessel/unloadVessel/unloadVesselHistory"

			}
		]
	},

2. Configure dynamic tabBar.js

As shown in the picture↓

 Code ↓

// 装船权限
const loadVessel = [{
		"pagePath": "/pages/loadAndUnloadVessel/loadVessel/loadVesselPlan",
		"text": "装船作业计划",
		"iconPath": require("@/static/img/common/装船作业计划.png"),
		"selectedIconPath": require("@/static/img/common/装船作业计划1.png")
	}, {
		"pagePath": "/pages/loadAndUnloadVessel/loadVessel/loadVesselCommand",
		"text": "装船指令",
		"iconPath": require("@/static/img/common/装船指令.png"),
		"selectedIconPath": require("@/static/img/common/装船指令1.png")
	},
	{
		"pagePath": "/pages/loadAndUnloadVessel/loadVessel/loadVesselHistory",
		"text": "历史",
		"iconPath": require("@/static/img/common/历史.png"),
		"selectedIconPath": require("@/static/img/common/历史1.png")
	}
]

//卸船权限
const unloadVessel = [{
		"pagePath": "/pages/loadAndUnloadVessel/unloadVessel/unloadVesselPlan",
		"text": "卸船作业计划",
		"iconPath": require("@/static/img/common/装船作业计划.png"),
		"selectedIconPath": require("@/static/img/common/装船作业计划1.png")
	}, {
		"pagePath": "/pages/loadAndUnloadVessel/unloadVessel/unloadVesselCommand",
		"text": "卸船指令",
		"iconPath": require("@/static/img/common/装船指令.png"),
		"selectedIconPath": require("@/static/img/common/装船指令1.png")
	},
	{
		"pagePath": "/pages/loadAndUnloadVessel/unloadVessel/unloadVesselHistory",
		"text": "历史",
		"iconPath": require("@/static/img/common/历史.png"),
		"selectedIconPath": require("@/static/img/common/历史1.png")
	}
]

export default {
	loadVessel,//装船权限名  最后要存入 isMemberType里
	unloadVessel//卸船权限名 最后要存入 isMemberType里
}

3. Use vuex to perform a storage assignment on the tabBar list data

index.js↓

import Vue from 'vue'
import Vuex from 'vuex'
import tabBar from './modules/tabBar'
Vue.use(Vuex)

const store = new Vuex.Store({
	modules:{tabBar},
	state: {
	
	},
	getters: {
		tabBarList: state => state.tabBar.tabBarList,
		isMemberType: state => state.tabBar.isMemberType,
	},
	mutations: {
		
	}
})

export default store

tabBar.js↓

import tarBarUserType from '@/utils/tabBar.js';

const tabBar = {
	state: {
		// 判断当前权限
		isMemberType: '',
		// tabbar列表数据
		tabBarList: []

	},
	mutations: {
		setType(state, isMemberType) {
			state.isMemberType = isMemberType;
			state.tabBarList = tarBarUserType[isMemberType];//根据权限取出对应的tabBar
			console.log(state.tabBarList )
		}
	}
}

export default tabBar;

Create a tabBar component↓

 Code ↓

<template>
	<view class="tab-bar">
		<view class="content">
			<view class="one-tab" v-for="(item, index) in tabBarList" :key="index" @click="selectTabBar(item.pagePath)">
				<view>
					<view class="tab-img">
						<image v-if="routePath === item.pagePath" class="img" :src="item.selectedIconPath"></image>
						<image v-else class="img" :src="item.iconPath"></image>
					</view>
				</view>
				<view class="tit">{
   
   { item.text }}</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			// 底部导航栏数据
			tabBarList: {
				type: Array,
				required: true
			},
			// 当前页面路径
			routePath: {
				type: String,
				required: true
			}
		},
		data() {
			return {};
		},
		methods: {
			selectTabBar(path) {
			
				// console.log(path)
				uni.switchTab({
					url: path
				})
			}
		},
		onLoad() {
			// console.log(this.tabBarList)
		}
	};
</script>

<style lang="scss">
	.tab-bar {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100vw;
		padding: 0rpx;
		// padding-bottom: calc(10rpx + constant(safe-area-inset-bottom));
		// padding-bottom: calc(10rpx + env(safe-area-inset-bottom));
		background-color: #fff;
		// background-color: red;
		// height:80rpx;


		.content {
			display: flex;
			flex-direction: row;

			.one-tab {

				display: flex;
				flex-direction: column;
				align-items: center;
				width: 100%;
				// background-color: pink;

				.tab-img {
					width: 50rpx;
					height: 50rpx;

					.img {
						width: 100%;
						height: 100%;
					}
				}

				.tit {
					font-size: 25rpx;
					transform: scale(0.7);
				}
			}
		}
	}
</style>

5. Components need to be introduced in pages with tabbars and relevant data passed in (routePath passes in the tabbar path of the current page)

<template>
	<view class="content">
		卸船指令
		
		<tabBarAction :tabBarList='tabBarList' routePath='/pages/loadAndUnloadVessel/unloadVessel/unloadVesselCommand'>
		</tabBarAction>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapState
	} from 'vuex';
	import tabBarAction from '@/components/tabBarAction/tabBarAction'

	export default {
		data() {
			return {

			};
		},
		computed: {
			// 这里的tabBarList就是数据源,直接使用传值
			...mapGetters(['tabBarList'])
		},
		components: {
			tabBarAction

		},
	}
</script>


<style lang="scss" scoped>
	.content {
		margin-top: 100rpx;
		height: 100%;


	}
</style>

6. Configure permissions where needed: ↓

this.$store.commit('setType', tabbar路径);
                uni.switchTab({
                    url:tabbar路径
                })

7. Hide tabBar in app.vue

		onShow() {
			uni.hideTabBar({});
		},

Refer to this article, the re-plumping process↓

Mini program customizes the tabbar navigation bar and dynamically controls the tabbar function implementation (uniapp)_uniapp dynamically sets the tabbar_Don’t fix my bug blog-CSDN blog

Guess you like

Origin blog.csdn.net/lanbaiyans/article/details/130398923