自定义tabbar(custom-tab-bar)根据身份动态展示个数,第一次setData不好用?
发布于 4 年前 作者 ocao 5845 次浏览 来自 官方Issues

uniapp中,我想根据登录身份不同展示不同的tabbar,现在采取的是custom-tab-bar方式:

现在有如下问题:第一次编译,setData不好用,页面未渲染指定的tabbar

原因:编译时候,console.log(this.data.list)是有值的,但是页面wxml输出{{list.length}}是0。导致tabbar渲染不出来;

如果上一次登录的是身份1,退出进行身份2的登录,还是会显示身份1的tabbar,点击别的tab,才会变成身份2的tabbar.

不知道为什么setData渲染不到页面中?

data: {
		selected: 0,
		color: "#7A7E83",
		selectedColor: "#007AFF",
		list: [],
		companyList: [{
				"pagePath": "/pages/boat/boat",
				"iconPath": "../static/boat.png",
				"selectedIconPath": "../static/boat-selected.png",
				"text": "船舶"
			},
			{
				"pagePath": "/pages/crew/home/home",
				"iconPath": "../static/order.png",
				"selectedIconPath": "../static/order-selected.png",
				"text": "船员",
			},
			{
				"pagePath": "/pages/order/order/order",
				"iconPath": "../static/order.png",
				"selectedIconPath": "../static/order-selected.png",
				"text": "订单"
			}, {
				"pagePath": "/pages/mine/mine",
				"iconPath": "../static/mine.png",
				"selectedIconPath": "../static/mine-selected.png",
				"text": "我的"
			}
		],
		crewList: [{
				"pagePath": "/pages/boat/boat",
				"iconPath": "../static/boat.png",
				"selectedIconPath": "../static/boat-selected.png",
				"text": "船舶"
			},
			{
				"pagePath": "/pages/order/order/order",
				"iconPath": "../static/order.png",
				"selectedIconPath": "../static/order-selected.png",
				"text": "订单"
			}, {
				"pagePath": "/pages/mine/mine",
				"iconPath": "../static/mine.png",
				"selectedIconPath": "../static/mine-selected.png",
				"text": "我的"
			}
		]
	},
lifetimes: {
attached() {
			try {
				var value = wx.getStorageSync('userInfo')
				if (value) {
					console.log(value.userLevel)
					if (value.userLevel == 2) {
						// 公司
						this.setData({
							list: this.data.companyList,
							userLevel: value.userLevel
						})
						console.log(this.data.list)
					} else if (value.userLevel == 3) {
						let crewList =  JSON.parse(JSON.stringify(this.data.companyList))
						crewList.splice(1, 1)
						this.setData({
							list: crewList,
							userLevel: value.userLevel
						})
			
					} else {
						this.setData({
							list: [],
						})
					}
					 this.onLoad()
				}
			
			} catch (e) {}
		},
}
1 回复

如果在该页面 attached生命周期中 直接赋值,第一次也是可以的。加一层localstorage取值判断,为什么就不行了呢~

回到顶部