用这个组件自定义tabbar
源代码:
<template>
<view>
<view>首页</view>
<custom-tab-bar :items="tabItems" :currentIndex="currentTab" @item-click="onTabClick"
:color="color" :selectedColor="selectedColor"></custom-tab-bar>
</view>
</template>
<script>
import CustomTabBar from '@/uni_modules/niceui-tabBar/components/niceui-tabBar/niceui-tabBar.vue';
import tabbar from '@/uni_modules/niceui-tabBar/common/tabbar.js'
export default {
components: {
CustomTabBar
},
data() {
return {
currentTab: 0, // 当前选中的tab索引
tabItems:tabbar.tabItems,
color:tabbar.color,
selectedColor:tabbar.selectedColor
}
},
methods: {
onTabClick(index) { // 切换tab的函数,当选中某个tab时触发
//this.currentTab = index;
if(index!=this.currentTab){
uni.redirectTo({
url:this.tabItems[index].pagePath
})
}
}
}
};
</script>
//pagePath以自己项目的实际路径为准,这里是为了代码聚合在一起
const tabbar = {
"tabItems":[
{
"pagePath": "/uni_modules/niceui-tabBar/pages/index/index",
"icon": '/static/images/tabbar/index.png',
"selectedIcon":'/static/images/tabbar/index-action.png',
"text": '首页',
},
{
"pagePath": "/uni_modules/niceui-tabBar/pages/add/add",
"icon": '/static/images/tabbar/add.png',
"selectedIcon":'/static/images/tabbar/add-action.png',
"text": '新建',
"up":true
},
{
"pagePath": "/uni_modules/niceui-tabBar/pages/user/user",
"icon": '/static/images/tabbar/me.png',
"selectedIcon":'/static/images/tabbar/me-action.png',
"text": '我的',
}
],
"color":"#333",
"selectedColor":"green",
}
export default tabbar
跳转报错
解决:
问题是用的redirectTo跳转,redirectTo可以在当前页面跳转到指定的目标页面,当前页面会被关闭,并跳转到指定的目标页面。
而我们是底部导航栏页面切换,应该用switchTab