diff --git a/src/platforms/app-plus/service/api/ui/tab-bar.js b/src/platforms/app-plus/service/api/ui/tab-bar.js index c835c9cd49e5693966c9eb0507317dc96331b590..c5510fba15d45b4d131eba4f7d9b587b545e13ae 100644 --- a/src/platforms/app-plus/service/api/ui/tab-bar.js +++ b/src/platforms/app-plus/service/api/ui/tab-bar.js @@ -20,9 +20,10 @@ export function setTabBarItem ({ text, iconPath, selectedIconPath, - pagePath + pagePath, + visible }) { - tabBar.setTabBarItem(index, text, iconPath, selectedIconPath) + tabBar.setTabBarItem(index, text, iconPath, selectedIconPath, visible) const route = pagePath && __uniRoutes.find(({ path }) => path === pagePath) if (route) { const meta = route.meta diff --git a/src/platforms/app-plus/service/framework/tab-bar.js b/src/platforms/app-plus/service/framework/tab-bar.js index e056dfd129019eac17357704114d64fb25bc78ae..3b14da16f94c44bb561e7705e37f0d3aff800853 100644 --- a/src/platforms/app-plus/service/framework/tab-bar.js +++ b/src/platforms/app-plus/service/framework/tab-bar.js @@ -49,7 +49,7 @@ function setTabBarBadge (type, index, text) { /** * 动态设置 tabBar 某一项的内容 */ -function setTabBarItem (index, text, iconPath, selectedIconPath) { +function setTabBarItem (index, text, iconPath, selectedIconPath, visible) { const item = { index } @@ -62,7 +62,17 @@ function setTabBarItem (index, text, iconPath, selectedIconPath) { if (selectedIconPath) { item.selectedIconPath = getRealPath(selectedIconPath) } - tabBar && tabBar.setTabBarItem(item) + if (visible !== undefined) { + item.visible = config.list[index].visible = visible + delete item.index + + const tabbarItems = config.list.map(item => ({ visible: item.visible })) + tabbarItems[index] = item + + tabBar && tabBar.setTabBarItems({ list: tabbarItems }) + } else { + tabBar && tabBar.setTabBarItem(item) + } } /** * 动态设置 tabBar 的整体样式 diff --git a/src/platforms/h5/components/app/tabBar.vue b/src/platforms/h5/components/app/tabBar.vue index 9439fdd0a53fce37f8ce440dd46cf86de9339ad8..d1be1eb5c069658517601f1c292b79323721cf3b 100644 --- a/src/platforms/h5/components/app/tabBar.vue +++ b/src/platforms/h5/components/app/tabBar.vue @@ -12,7 +12,7 @@ class="uni-tabbar-border" />
to.meta.pagePath === item.pagePath) - if (index > -1) { - this.selectedIndex = index - } - } + handler () { + // 只在此做一次 visibleList 的初始化 + if (!this.visibleList.length) this._initVisibleList() + this.setSelectedIndex() + } + }, + list: { + deep: true, + handler () { + this._initVisibleList() + this.setSelectedIndex() } } }, created () { - this._initMidButton() + this.list.forEach(item => { + if (item.visible === undefined) { + this.$set(item, 'visible', true) + } + }) }, beforeCreate () { this.__path__ = this.$route.path }, methods: { - _getRealPath (filePath) { + getIconPath (item, index) { + return (this.selectedIndex === index ? item.selectedIconPath || item.iconPath : item.iconPath) || '' + }, + setSelectedIndex () { + if (this.$route.meta.isTabBar) { + this.__path__ = this.$route.path + const index = this.visibleList.findIndex(item => this.$route.meta.pagePath === item.pagePath) + this.selectedIndex = index + } + }, + _initVisibleList () { + this.visibleList = this._initMidButton(this.list.filter(item => item.visible !== false)) + }, + _getRealPath (filePath = '') { const SCHEME_RE = /^([a-z-]+:)?\/\//i const DATA_RE = /^data:.*,.*/ if (!(SCHEME_RE.test(filePath) || DATA_RE.test(filePath)) && filePath.indexOf('/') !== 0) { @@ -375,8 +396,8 @@ export default { UniServiceJSBridge.emit('onTabItemTap', detail) } }, - _initMidButton () { - const listLength = this.list.length + _initMidButton (list) { + const listLength = list.length // 偶数则添加midButton if (listLength % 2 === 0 && isPlainObject(this.midButton)) { // 给midButton赋值默认值 @@ -388,8 +409,9 @@ export default { for (const key in DefaultMidButton) { this.midButton[key] = this.midButton[key] || DefaultMidButton[key] } - this.list.splice(~~(listLength / 2), 0, Object.assign({}, this.midButton, { isMidButton: true })) + list.splice(~~(listLength / 2), 0, Object.assign({}, this.midButton, { isMidButton: true })) } + return list }, _uniTabbarBdStyle (item) { return Object.assign({}, { diff --git a/src/platforms/h5/service/api/ui/tab-bar.js b/src/platforms/h5/service/api/ui/tab-bar.js index 7118afd8ea238d80f95c41da2cb7d1d314b05e64..977aae00dbefd61592be07a9825545016f5bf2a1 100644 --- a/src/platforms/h5/service/api/ui/tab-bar.js +++ b/src/platforms/h5/service/api/ui/tab-bar.js @@ -2,7 +2,7 @@ import { setProperties } from 'uni-shared' -const setTabBarItemProps = ['text', 'iconPath', 'selectedIconPath'] +const setTabBarItemProps = ['text', 'iconPath', 'selectedIconPath', 'visible'] const setTabBarStyleProps = ['color', 'selectedColor', 'backgroundColor', 'borderStyle']