需求: 頁面中給定tabbar,要求根據當前選中tabbar改變頂部導航欄title
tabbar組件中傳入數組即為tabbar項目,因為可以拿到tabar各項,使用@clickHandle接收tabbar傳遞的事件,參數為tabbarItem的index
分兩步:
- :根據文檔要求,onReady中使用uni.setNavigationBarTitle,在頁面渲染時設置當前頂部導航欄內容
// 剛進入時設置為第0項
onReady() {
let _this = this
uni.setNavigationBarTitle({
title:_this.tabBarItem[0]
})
},
- : 設置在點擊tabbar時再次設置當前index的內容為頂部tabbar的內容
// 切換時使用索引
toggleTabBar(index) {
this.currentindex = index
// 在初始化時配置為0,將當前的tabbaritem項變成當前的navbar名
let _this = this
uni.setNavigationBarTitle({
title:_this.tabBarItem[index]
})
},
這樣,就完成了相應需求。
以上。