系列导航

一、vue tabBar导航栏设计实现1-初步设计

二、vue tabBar导航栏设计实现2-抽取tab-bar

三、vue tabBar导航栏设计实现3-进一步抽取tab-item

四、vue tabBar导航栏设计实现4-再次抽取MainTabBar

五、vue tabBar导航栏设计实现5-最终版本

tabBar导航栏设计5-最终版本

一、     本节目标效果

导航栏选中哪个哪个用红色图标,并且字体的颜色可以随意设置(这里为了醒目用蓝色)

二、代码结构

注:主要是标红的几个文件

三、代码

App.vue

<template>
<div id="app">
<main-tab-bar></main-tab-bar>
<router-view></router-view>
</div>
</template> <script>
import {
defineComponent
} from 'vue'
import MainTabBar from './components/mainTabbar/MainTabBar'
export default defineComponent({
//组件名称
name: 'App',
//接收父组件的数据
props: {},
components: {
MainTabBar
},
setup(props, ctx) {
return {}
}
})
</script> <style lang="scss">
@import "./assets/css/base.css";
</style>

TabBar.vue

<template>
<div id="tab-bar">
<slot></slot>
</div>
</template> <script> import {defineComponent} from 'vue' export default defineComponent({
//组件名称
name:'TabBar',
//接收父组件的数据
props:{
},
components: { },
setup(props,ctx){
return{
}
}
}) </script> <style lang="scss">
#tab-bar {
display: flex;
background-color: #f6f6f6; position: fixed;
left: 0;
right: 0;
bottom: 0; box-shadow: 0 -1px 1px rgba(100,100,100,.2);
} </style>

TabBarItem.vue

<template>
<div class="tab-bar-item" @click="itemClick">
<div v-if="isActive"><slot name="item-icon-active"></slot></div>
<div v-else><slot name="item-icon"></slot></div>
<div :style="activeStyle"><slot name="item-text"></slot></div>
</div>
</template> <script>
import { createRouter, createWebHistory } from 'vue-router'
import {defineComponent} from 'vue' export default defineComponent({
//组件名称
name:'TabBarItem',
//接收父组件的数据
props:{
path: String,
activeColor: {
type: String,
default: 'red'
}
},
components: { },
computed: {
isActive() {
//不等于-1就是找到了
return this.$route.path.indexOf(this.path) !== -1
},
activeStyle() {
return this.isActive ? {color: this.activeColor} : {}
}
},
methods: {
itemClick() {
this.$router.push(this.path)
}
},
setup(props,ctx){
return{ }
}
})
</script> <style lang="scss">
.tab-bar-item {
flex: 1;
text-align: center;
height: 49px;
font-size: 14px;
} .tab-bar-item img {
width: 24px;
height: 24px;
margin-top: 3px;
vertical-align: middle;
margin-bottom: 2px;
}
</style>

MainTabBar.vue

<template>
<tab-bar>
<tab-bar-item path="/home" activeColor="blue">
<template v-slot:item-icon>
<img :src="require('../../assets/img/tabbar/home.svg')">
</template>
<template v-slot:item-icon-active>
<img :src="require('../../assets/img/tabbar/home_active.svg')">
</template>
<template v-slot:item-text>
<div slot="item-text">首页</div>
</template>
</tab-bar-item>
<tab-bar-item path="/category" activeColor="blue">
<template v-slot:item-icon>
<img :src="require('../../assets/img/tabbar/category.svg')">
</template>
<template v-slot:item-icon-active>
<img :src="require('../../assets/img/tabbar/category_active.svg')">
</template>
<template v-slot:item-text>
<div slot="item-text">分类</div>
</template>
</tab-bar-item>
<tab-bar-item path="/cart" activeColor="blue">
<template v-slot:item-icon>
<img :src="require('../../assets/img/tabbar/shopcart.svg')">
</template>
<template v-slot:item-icon-active>
<img :src="require('../../assets/img/tabbar/shopcart_active.svg')">
</template>
<template v-slot:item-text>
<div slot="item-text">购物车</div>
</template>
</tab-bar-item>
<tab-bar-item path="/profile" activeColor="blue">
<template v-slot:item-icon>
<img :src="require('../../assets/img/tabbar/profile.svg')">
</template>
<template v-slot:item-icon-active>
<img :src="require('../../assets/img/tabbar/profile_active.svg')">
</template>
<template v-slot:item-text>
<div slot="item-text">我的</div>
</template>
</tab-bar-item>
</tab-bar>
</template> <script>
import TabBar from '../tabbar/TabBar'
import TabBarItem from '../tabbar/TabBarItem' export default {
name: "MainTabBar",
components: {
TabBar,
TabBarItem
}
}
</script> <style scoped> </style>

index.js

import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import Category from '../views/Category.vue'
import Cart from '../views/Cart.vue'
import Profile from '../views/Profile.vue' const routes = [
{
path: '/home',
name: 'Home',
component: Home
} ,
{
path: '/category',
component: Category
},
{
path: '/cart',
component: Cart
},
{
path: '/profile',
component: Profile
}
] const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes
}) export default router

其他一些代码不很简单看之前的博客内容

四、代码按照步骤解释

1、 抽取MainTabBar.vue组件

MainTabBar.vue组件里<tab-bar-item>标签上增加了activeColor="blue"这属性,这里的颜色可以根据需求随意换。

2、 TabBarItem.vue组件里增加isActive和activeStyle计算属性用来控制,哪个按钮选中就用红色的图标和字体颜色动态设置。

this.$route.path.indexOf(this.path) !== -1  //当前路径是哪个判断,不等于-1就是找到了

vue tabBar导航栏设计实现5-最终版本的更多相关文章

  1. 微信小程序------导航栏样式、tabBar导航栏

    一:导航栏样式设置 小程序的导航栏样式在app.json中定义. 这里设置导航,背景黑色,文字白色,文字内容测试小程序 app.json内容: { "pages":[ " ...

  2. 微信小程序入门四: 导航栏样式、tabBar导航栏

    实例内容 导航栏样式设置 tabBar导航栏 实例一:导航栏样式设置 小程序的导航栏样式在app.json中定义. 这里设置导航,背景黑色,文字白色,文字内容测试小程序 app.json内容: { & ...

  3. 超详细Vue实现导航栏绑定内容锚点+滚动动画+vue-router(hash模式可用)

    超详细Vue实现导航栏绑定内容锚点+滚动动画+vue-router(hash模式可用) 转载自:https://www.jianshu.com/p/2ad8c8b5bf75 亲测有效~ <tem ...

  4. Nuxt/Vue自定义导航栏Topbar+标签栏Tabbar组件

    基于Vue.js实现自定义Topbar+Tabbar组件|仿咸鱼底部凸起导航 最近一直在倒腾Nuxt项目,由于Nuxt.js是基于Vue.js的服务端渲染框架,只要是会vue,基本能很快上手了. 一般 ...

  5. 记一次Vue跨导航栏问题解决方案

    简述 这篇文章是我项目中,遇到的一个issue,我将解决过程和方法记录下来. 本篇文章基于Vue.js进行的前端页面构建,由于仅涉及前端,将不做数据来源及其他部分的叙述.使用的CSS框架是 Boots ...

  6. 使用vue给导航栏添加链接

    如下面的导航栏,使用vue技术给该导航栏增加链接: js代码为: navigation:function(){ new Vue({ el: '#navUl', data: { menuData:{ ' ...

  7. Flutter - TabBar导航栏切换后,状态丢失

    上一篇讲到了 Flutter - BottomNavigationBar底部导航栏切换后,状态丢失 里面提到了TabBar,这儿专门再写一下吧,具体怎么操作,来不让TabBar的状态丢失.毕竟大家99 ...

  8. Vue设置导航栏为公共模块并在登录页不显示

    1.公共模块的内容可以放在App.vue中但是通常登录页面是不需要导航的,那么就需要规避登录页这时,就可以采用keep-alive结合$route.meta来实现这个功能.keep-alive 是 V ...

  9. 新浪微博客户端(1)-实现Tabbar导航栏效果

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launc ...

  10. vue 侧边导航栏递归显示

    import axios from "axios"; import tabs1 from "../tab_content/tab1.vue"; import m ...

随机推荐

  1. Excel表格数据可视化的六大常见方式,看看你都会吗?

    当涉及到Excel表格数据的可视化,有许多不同的方式可以展示和呈现数据.以下是六种常见的Excel表格数据可视化方式的详细介绍. 1. 条形图(Bar Chart) 条形图是一种常见的数据可视化图表类 ...

  2. Java+Selenium爬取高德POI边界坐标

    一.写在前面 关于爬取高德兴趣点边界坐标网上有几篇文章介绍实现方式,总的来说就是通过https://www.amap.com/detail/get/detail传入POI的ID值获取数据,BUT,如果 ...

  3. Net 高级调试之十二:垃圾回收机制以及终结器队列、对象固定

    一.简介 今天是<Net 高级调试>的第十二篇文章,这篇文章写作时间的跨度有点长.这篇文章我们主要介绍 GC 的垃圾回收算法,什么是根对象,根对象的存在区域,我们也了解具有析构函数的对象是 ...

  4. Javascript Ajax总结——HTTP头部信息

    每个HTTP请求和响应都会带有相应的头部信息,其中有的对开发人员有用,有的没用.XHR对象也提供了操作这两种头部(即请求头部和响应头部)信息的方法.默认情况下,在发送XHR请求的同时,还会发送下列头部 ...

  5. 制造业工厂生产管理MES系统中的设备管理模块

    制造业工厂万界星空科技生产管理MES系统中的设备管理模块介绍: 随时工厂数字化建设的大力推进,设备管理的效率得到了很大的提升,特别是作为机加工企业,设备是整个企业非常重要的核心资产. 1.MES设备管 ...

  6. keycloak~从login-status-iframe页面总结如何跨域传值~续

    keycloak~从login-status-iframe相关文章,可阅读我的这两篇keycloak~从login-status-iframe页面总结如何跨域传值,keycloak~对接login-s ...

  7. BeanCurrentlyInCreationException解决当前容器创建异常、循环依赖问题

    BeanCurrentlyInCreationException解决当前容器创建异常.循环依赖问题 一.什么是循环依赖呢? 类A依赖类B,类B也依赖类A,这种情况就会出现循环依赖. Bean A → ...

  8. ASR项目实战-架构设计

    一般而言,业务诉求作为架构设计的输入. 需求清单 对于语音识别产品而言,需满足的需求,举例如下: 功能需求 文件转写. 长文件转写,时长大于60秒,小于X小时,X可以指定为5. 短文件转写,时长小于6 ...

  9. 【ASP.NET Core】使用SignalR推送服务器日志

    一个多月前接手了一个产线机器人项目,上位机以读写寄存器的方式控制机器人,服务器就是用 ASP.NET Core 写的 Web API.由于前一位开发者写的代码质量问题,导致上位机需要16秒才能启动.经 ...

  10. 新一代通信协议 - Socket.D

    一.简介 Socket.D 是一种二进制字节流传输协议,位于 OSI 模型中的5~6层,底层可以依赖 TCP.UDP.KCP.WebSocket 等传输层协议.由 Noear 开发.支持异步流处理.其 ...