收藏
回答

自定义tabbar(custom-tab-bar)根据身份动态展示个数,第一次setData不好用?

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 个回答

  • Flowcy🐳
    Flowcy🐳
    2020-12-24

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

    2020-12-24
    有用
    回复
登录 后发表内容
问题标签