用Vue封装导航栏组件
作者:Huangrunze! 发布时间:2023-07-02 16:51:46
前言:把一个功能模块使用组件化的思想充分封装,如导航栏,这无论对我们的开发思想还是效率都有许多好处,在开发中,我们要尽量多得运用组件化的开发思想,不要把所有代码都写在同一个.vue文件中,这样能大大提高代码的可读性。
封装导航栏
主要思路:把红色的部分当成一个个组件,而他们只是图片和文字不同,所以我们可以把他们封装成同一个组件,然后向组件里传入图片信息和文字信息即可(可以用插槽)。
//TabBarItem.vue
<template>
<div class="tabBarItem" @click="tabBarItemClick">
<div v-if="!isActive">
<slot name="item-icon"></slot>
</div>
<div v-else>
<slot name="item-icon-active"></slot>
</div>
<div :style="isActiveColor">
<slot name="item-text"></slot>
</div>
</div>
</template>
<script>
export default {
name:"TabBarItem",
props:{
path:String,
activeColor:{
type:String,
default:"pink"
}
},
computed:{
isActive:{
get(){
return this.$route.path.indexOf(this.path)!==-1;
},
set(){}
},
isActiveColor(){
return this.isActive?{color:this.activeColor}:{}
}
},
methods:{
tabBarItemClick(){
this.$router.push(this.path);
}
}
}
</script>
<style scoped>
.tabBarItem{
flex: 1;
font-size: 12px;
}
.tabBarItem img{
margin-top: 3px;
width: 24px;
padding-bottom:3px ;
}
</style>
接下来就是封装一个把这4个选项放在同一个地方的容器。
//TabBar.vue
<template>
<div class="tabBar">
<slot></slot>
</div>
</template>
<script>
export default {
name:"TabBar"
}
</script>
<style scoped>
.tabBar{
display: flex;
height: 49px;
position: fixed;
left: 0;
right: 0;
bottom: 0;
text-align: center;
box-shadow: 0px -1px 1px rgba(100, 100, 100, .1);
background-color: #f6f6f6;
}
</style>
再接下来就是使用了,给每一个不同的TabBarItem的插槽写入不同的图片和文字信息。
//MainTabBar.vue
<template>
<div class="mainTabBar">
<tab-bar>
<tab-bar-item path="/home" activeColor="#ff8198">
<img src="~assets/img/tabbar/home.svg" alt="" slot="item-icon">
<img src="~assets/img/tabbar/home_active.svg" alt="" slot="item-icon-active">
<div slot="item-text">首页</div>
</tab-bar-item>
<tab-bar-item path="/category" activeColor="#ff8198">
<img src="~assets/img/tabbar/category.svg" alt="" slot="item-icon">
<img src="~assets/img/tabbar/category_active.svg" alt="" slot="item-icon-active">
<div slot="item-text">分类</div>
</tab-bar-item>
<tab-bar-item path="/cart" activeColor="#ff8198">
<img src="~assets/img/tabbar/shopcart.svg" alt="" slot="item-icon">
<img src="~assets/img/tabbar/shopcart_active.svg" alt="" slot="item-icon-active">
<div slot="item-text">购物车</div>
</tab-bar-item>
<tab-bar-item path="/profile" activeColor="#ff8198">
<img src="~assets/img/tabbar/profile.svg" alt="" slot="item-icon">
<img src="~assets/img/tabbar/profile_active.svg" alt="" slot="item-icon-active">
<div slot="item-text">我的</div>
</tab-bar-item>
</tab-bar>
</div>
</template>
<script>
import TabBar from "components/common/tabbar/TabBar"
import TabBarItem from "components/content/tabbar/TabBarItem"
export default {
name:"MainTabBar",
components:{
TabBar,
TabBarItem
}
}
</script>
<style>
</style>
导航栏一般都在主页中使用,所以我们把这个导航栏放在App.vue
<template>
<div id="app">
<main-tab-bar></main-tab-bar>
</div>
</template>
<script>
import MainTabBar from "components/content/tabbar/MainTabBar";
export default {
name: 'App',
components:{
MainTabBar
}
}
总结:这样看来,我们写一个导航栏用了3个文件,这可能看起来是麻烦的,但这也大大提高了代码的可读性,如果我们还需要在该项目别的地方使用导航栏,我们只需要直接创建一个MainTabBar类似的文件,然后把你要的图片和文字写进入即可,甚至于在别的项目用到时,我们可以直接将文件拷贝过去就能够直接使用,连CSS样式都不需要我们去写,这就大大提高了我们的开发效率。
来源:https://blog.csdn.net/weixin_45805570/article/details/115621055?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EOPENSEARCH%7Edefault-3.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EOPENSEARCH%7Edefault-3.no_search_link
猜你喜欢
- 有两种方法供参考.一种是实实在在的将图片先用ASPJPEG组件做成想要的尺寸,再读取好处是节省服务器带宽,速度快坏处是处理起来较为麻烦,并网
- 本文实例讲述了Django框架首页和登录页分离操作。分享给大家供大家参考,具体如下:1.登录模板login.html<!DOCTYPE
- 1.主要用到 QFileDialog 方法打开本地文件2.界面打开前:打开后:3. 代码import sysfrom PyQt5 impor
- 1.我的MySQL中的start_time存储的是2018-03-21 10:55:32格式的时间,我需要按照YYYY-MM-DD格式来查询
- MySql explain语句的返回结果中,filtered字段要怎么理解?MySql5.7官方文档中描述如下:The filte
- Windows下的安装:下载地址:https://pypi.python.org/pypi/pyquery/#downloads下载后安装:
- 前言项目需求是跟用户当前位置判断是否在给定的地理位置范围内,符合位置限制才可以打卡,其中的位置范围是一个或多个不规则的多边形。如下图,判断用
- 我们有时候会需要在网上查找并下载图片,当数量比较少的时候,点击右键保存,很轻松就可以实现图片的下载,但是有些图片进行了特殊设置,点击右键没有
- 单例模式是一个经典设计模式,简要的说,一个类的单例模式就是它只能被实例化一次,实例变量在第一次实例化时就已经固定。 在Python
- shapefile转换geojsonimport shapefileimport codecsfrom json import dumps#
- 今天做了一个很简单的小项目,感受到了paramiko模块的强大,也深感自己Linux的功力不行~~一、需求二、简单需求分析及流程图需求很少,
- 一、引言网络上充满了窃听,我们的信息很容易被不怀好意的人获得,给我们造成不好的影响。如果你需要在网络上传输机密或者敏感的隐私信息,为了防备别
- request获取post请求中的json数据def hello(request): data = json.loads(request.b
- 在有些情况下,利用try…except来捕捉异常可以起到代替if…else的作用。比如在判断一个链表是否存在环的leetcode题目中,初始
- SQL 多条件查询以后我们做多条件查询,一种是排列结合,另一种是动态拼接SQL如:我们要有两个条件,一个日期@addDate,一个是@nam
- 我的数据库和报表服务的版本如下:数据库:SQL Server 2008 R2报表服务:SQL Server 2008 R2 Reportin
- 实战场景经常有朋友问,学 Python 面向对象时,翻阅别人代码,会发现一个 super() 函数,那这个函数的作用到底是什么?super(
- 在许多用SQL Server实现的新的企业系统设计中,系统设计师需要在给数据结构和管理应用程序逻辑的定位上做出具有关键性意义的决定。SQL
- 一、概述任务描述:开发一个程序,用于获取局域网中开启snmp服务的主机ip地址列表,并写入相应文件以便其它程序使用。背景知识:SNMP是基于
- 在Numpy中经常使用到的操作由扁平化操作,Numpy提供了两个函数进行此操作,他们的功能相同,但在内存上有很大的不同.先来看这两个函数的使