vue3下H5项目实现一个怎么实现tabbar【详细步骤】

  1. vue 安装
npm create vue

注意:需要安装路由 vue-router
2. 定义主要页面
比如:

import Home from '../page/Home.vue'
import Discover from '../page/Discover.vue'
import Cate from '../page/Cate.vue'
import Me  from '../page/Me.vue'

const routes = [
    { path: '/home', component: Home },
    { path: '/cate', component: Cate },
    { path: '/discover', component: Discover },
    { path: '/me', component: Me },
]

结构如下
在这里插入图片描述
3. 定义一个tabbar 组件
在这里插入图片描述

<script setup>
import { useRoute } from 'vue-router'
// 当前路由
const route = useRoute()

const props = defineProps({
  path: {
    type: String,
    required: true,
    default: ''
  },
});

console.log(props.path);


const isActive = (path)=>{
  if (route.path === "/"){
    return "/home".indexOf(path) !== -1;
  }
  return route.path.indexOf(path) !== -1;
}
</script>

<template>
  <div id="tab-bar">
    <div class="tab-bar-item" :class="{active:isActive('/home')}">
      <RouterLink to="/home">
      <img src="@/assets/img/tabbar/home.svg" alt="...">
      <div class="item-text">首页</div>
      </RouterLink>
    </div>

    <div class="tab-bar-item" :class="{active:isActive('/cate')}">
      <RouterLink to="/cate">
      <img src="@/assets/img/tabbar/home.svg" alt="...">
      <div class="item-text" >分类</div>
      </RouterLink>
    </div>


    <div class="tab-bar-item" :class="{active:isActive('/discover')}">
      <RouterLink to="/discover">
      <img src="@/assets/img/tabbar/home.svg" alt="...">
      <div class="item-text">发现</div>
      </RouterLink>
    </div>


    <div class="tab-bar-item" :class="{active:isActive('/me')}">
      <RouterLink to="/me">
      <img src="@/assets/img/tabbar/home.svg" alt="...">
      <div class="item-text">我的</div>
      </RouterLink>
    </div>

  </div>
</template>

<style scoped>
#tab-bar{
  /* 流体布局,使得这个元素占据一行 */
  display: flex;
  height: 49px;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #f6f6f6;
  font-size: 14px;

  box-shadow: 0 -3px 1px rgba(100,100,100,.08);
}
.tab-bar-item.active .item-text{
  color: red;
}
.tab-bar-item{
  /* 使得这些元素均等分布在流体元素中 */
  flex: 1;
  text-align: center;
}
.tab-bar-item img{
  width: 24px;
  height: 24px;
  /* 使得图片下面不要离文字太远 */
  /* middle: 把当前盒的垂直中心和父级盒的基线加上父级的半x-height对齐 */
  vertical-align: middle;
}
</style>

  1. 在app.vue 中引入tabbar
<script setup>
import Tabbar from "@/components/Tabbar/Index.vue";
</script>
<template>
  <RouterView />
  <Tabbar></Tabbar>
</template>
<style scoped>
</style>

把本人的练习代码 有需要的自己看下
https://gitee.com/yu_yang_technology/htmlexercise

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
UniApp 是一款基于 Vue.js 的跨平台应用框架,可以同时开发运行在多个平台上的应用程序,包括 iOS、Android、H5 等。而 Vue 3 是 Vue.js 的最新版本,带来了许多新的特性和改进。 要自定义 UniApp 中的 TabBar,可以按照以下步骤进行操作: 1. 在 `pages.json` 配置文件中,找到 `tabBar` 字段。如果没有该字段,则手动添加一个空的 `tabBar` 字段。例如: ```json { "tabBar": {} } ``` 2. 在 `tabBar` 字段中,可以定义 TabBar 的整体样式和配置。例如: ```json { "tabBar": { "custom": true, // 开启自定义 TabBar "color": "#000000", // TabBar 的文字颜色 "selectedColor": "#ff0000", // 选中时的文字颜色 "backgroundColor": "#ffffff", // TabBar 的背景颜色 "list": [ { "pagePath": "pages/index/index", // 页面路径 "text": "首页", // 显示的文字 "iconPath": "static/tabbar/home.png", // 默认状态下的图标路径 "selectedIconPath": "static/tabbar/home_selected.png" // 选中状态下的图标路径 }, // 其他 TabBar 按照上述结构进行配置 ] } } ``` 3. 在项目目录下创建 `static` 目录,并在其中放置 TabBar 所需的图标文件。 4. 在对应页面中,可以使用 `uni.$emit` 方法来监听 TabBar 的切换事件。例如: ```vue <template> <view> <!-- 页面内容 --> </view> </template> <script> export default { onTabItemTap(item) { // 监听 TabBar 的切换事件 console.log(item.pagePath); // 当前页面的路径 console.log(item.text); // 当前 TabBar 的文字 } } </script> ``` 通过以上步骤,你就可以自定义 UniApp 中的 TabBar,并实现不同页面之间的切换效果了。希望对你有所帮助!如果还有其他问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

面线糊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值