Vue-CLI + Vue3 + Vue-Router4 实现tabbar小案例

news/2024/10/24 8:25:24/

Vue-CLI + Vue3 + Vue-Router4 实现tabbar小案例

tabbar导航栏案例:该案例实现了基础的组件封装,编程式路由,以及插槽的使用,对于我们日常组件化开发有着很大的启示作用,主要效果是点击下方的导航栏链接,上方内容区显示对应的组件内容,就如果我们常用的手机App一样。

效果图:

项目结构图:

svg图片资源下载:点击下载图片资源

1、 使用Vue-CLI新建tabbar工程,删除不必要的vue组件,并且取消代码检查:lintOnSave:false
2、 在assets文件夹下建立css和img文件夹,存放静态资源,将图片复制到img文件夹中的tabbar子文件夹中

3、 在css文件夹中建立base.css文件:

body{padding: 0;margin: 0;
}
/*然后在App.vue组件的style中引入css/base文件:@import "assets/css/base.css",此处不建议在main.js中引入*/

4、 修改App.vue的内容:

<template><div id="app"><div id="tab-bar"><div class="tab-bar-item">首页</div><div class="tab-bar-item">分类</div><div class="tab-bar-item">购物车</div><div class="tab-bar-item">我的</div></div></div>
</template><script>export default {name:'App',}
</script><style>
@import "assets/css/base.css";
#tab-bar{display: flex; /* 弹性盒子布局 */background-color: #f1f1f1;position: fixed;left: 0;right: 0;bottom: 0;height: 50px;box-shadow: 0 -2px 2px rgba(100,100,100,0.2);
}
.tab-bar-item{flex: 1; /* 对多项内容等分排列 */text-align: center;height: 49px;font-size: 14px;
}
</style>

5、 将导航栏封装成TabBar和TarBarItem两个组件,步骤如下:

//1. 把id为tab-bar的层及相应的css代码等封装在一起,作为一个组件
//2. 在components文件夹中建立一个名为tabbar的子文件夹,单独存放tabbar组件相关的组件,不要把所有的组件都直接放在components文件夹下,便于管理。[项目越大,组件越多]
//3. 在tabbar文件夹中建立名为TabBar.vue文件,把App.vue文件中的内容复制到TabBar.vue中
//4. 在TabBar.vue中,有四项内容,由于每一项在后期显示时有图片和文字,所以把TabBar.vue中的每一项封装成一个组件:TabBarItem.vue,考虑到TabBar.vue中使用的TabBarItem子组件数量是不固定的,所以要用到插槽。
//代码如下:

TabBar.vue内容:

<template><div id="tab-bar"><!-- 使用插槽,让App.vue根据需要,插入内容 --><slot></slot></div>
</template><script>
export default {name: "TabBar"
}
</script><style scoped>
#tab-bar{display: flex; /* 弹性盒子布局 */background-color: #f1f1f1;position: fixed;left: 0;right: 0;bottom: 0;height: 50px;box-shadow: 0 -2px 2px rgba(100,100,100,0.2);
}
</style>

TabBarItem.vue内容:

<template><div class="tab-bar-item"><img src="../../assets/img/tabbar/home.svg" alt="" /><div>首页</div></div>
</template><script>
export default {name: "TabBarItem"
}
</script><style scoped>
.tab-bar-item {flex: 1; /* 对多项内容等分排列 */text-align: center;height: 49px;font-size: 14px;
}.tab-bar-item img{width: 24px;height: 24px;margin-top: 3px;margin-bottom: 2px;vertical-align: middle;
}
</style>

App.vue内容:

<template><div id="app"><tab-bar><!-- 根据需要在插槽位置插入4个子组件TabBarItem[数量可变] --><tab-bar-item></tab-bar-item><tab-bar-item></tab-bar-item><tab-bar-item></tab-bar-item><tab-bar-item></tab-bar-item></tab-bar></div>
</template><script>//导入TabBar组件import TabBar from "./components/tabbar/TabBar";//导入TabBarItem组件import TabBarItem from "./components/tabbar/TabBarItem";export default {name:'App',components:{TabBarItem,TabBar}}
</script><style>
@import "assets/css/base.css";
</style>

6、 测试运行…界面会出现四个子组件,但内容都一样,而我们要实现的是显示不同内容的4个组件,所以就可以在TabBarItem中也使用插槽(上方显示图片,下方显示文字):

<template><div class="tab-bar-item"><!--  如果使用多个插槽,要使用具名插槽  --><slot name="item-icon"></slot><slot name="item-text"></slot></div>
</template>

7、 修改App.vue,将TabBarItem中的插槽填满,把其中对于img的样式放到App.vue中:

<template><div id="app"><tab-bar><!-- 根据需要在插槽位置插入4个子组件TabBarItem[数量可变] --><tab-bar-item><template #item-icon><img src="./assets/img/tabbar/home.svg" alt=""></template><template #item-text><div>首页</div></template></tab-bar-item><tab-bar-item><template #item-icon><img src="./assets/img/tabbar/category.svg" alt=""></template><template #item-text><div>分类</div></template></tab-bar-item><tab-bar-item><template #item-icon><img src="./assets/img/tabbar/shopcart.svg" alt=""></template><template #item-text><div>购物车</div></template></tab-bar-item><tab-bar-item><template #item-icon><img src="./assets/img/tabbar/profile.svg" alt=""></template><template #item-text><div>我的</div></template></tab-bar-item></tab-bar></div>
</template>
<style>
@import "assets/css/base.css";
.tab-bar-item img {width: 24px;height: 24px;margin-top: 3px;margin-bottom: 2px;vertical-align: middle;
}
</style>

接下来实现点击TabBarItem时,显示红色图片且红色文字。先给TabBarItem组件增加一个插槽,专门显示活跃图片,原来那个显示图片的插槽显示非活跃的;当组件被点击时,判断它是否为活跃状态,是就显示红色图片,否则显示黑色图片,文字也是如此…

8、 在TabBar组件的template中增加一个插槽,用来显示活跃图片:

<template><div class="tab-bar-item"><!--  如果使用多个插槽,要使用具名插槽  --><slot name="item-icon"></slot><slot name="item-icon-active"></slot><slot name="item-text"></slot></div>
</template>

9、 对App.vue进行修改,给每个tab-bar-item中插入一张活跃图片,然后运行测试,发现有两个svg图片,也就是说三个插槽都显示出来了:

<tab-bar-item><template #item-icon><img src="./assets/img/tabbar/home.svg" alt=""></template><template #item-icon-active><img src="./assets/img/tabbar/home_active.svg" alt=""></template><template #item-text><div>首页</div></template>
</tab-bar-item>

10、 对TabBarItem组件进行修改,实现两个图片插槽只显示一个,使用v-if与v-else实现:

<template><div class="tab-bar-item"><!--  如果使用多个插槽,要使用具名插槽  --><div v-if="!isActive"><slot name="item-icon"></slot></div><div v-else><slot name="item-icon-active"></slot></div><div :style="activeStyle"><slot name="item-text"></slot></div></div>
</template><script>
export default {name: "TabBarItem",data(){return{isActive:false}},computed:{activeStyle(){return this.isActive ? {color:'#ff5777'}:{}}}
}
</script><style scoped>
.tab-bar-item {flex: 1; /* 对多项内容等分排列 */text-align: center;height: 49px;font-size: 14px;
}
</style>

接下来实现路由:点击tabbar导航栏中某一项,就显示相应的组件。

在view文件夹中存放页面级别的组件,比如说路由跳转的组件,在component文件夹中存放可复用的组件。

11、 在views文件夹中创建四个子文件夹(home/category/shopcart/profile),在子文件夹中分别建立相对应的vue文件(HomeView/CategoryView/ShopCartView/ProfileView),文件中显示一些简单文字用以区分即可:

<template><div><h2>这是首页组件</h2></div>
</template>

12、 在index.js配置文件中添加路由配置信息:

{path: '/',redirect: '/home'
},
{path: '/home',name: 'home',component: () => import('../views/tabbar/home/HomeView')
},
{path: '/category',name: 'category',component: () => import('../views/tabbar/category/CategoryView')
},
{path: '/shopcart',name: 'shopcart',component: () => import('../views/tabbar/shopcart/ShopCartView')
},
{path: '/profile',name: 'profile',component: () => import('../views/tabbar/profile/ProfileView')
},

13、 在TabBarItem中添加点击事件与路由结合起来,用来显示正确的组件:

<script>
export default {name: "TabBarItem",props:{path:String,activeColor:String},data(){return{isActive:false}},computed:{activeStyle(){return this.isActive ? {color:this.activeColor}:{}}},methods:{itemClick(){this.$router.replace(this.path)}}
}
</script>

14、 对App.vue进行修改,在tab-bar-item上增加path参数(分别为:/home,/category,/shopcart,/profile),和activeColor参数(值为活跃时的颜色),然后在根模板下添加router-view输出组件,运行测试,此时就可以切换组件视图了:

<tab-bar-item path="/home" active-color="#ff5577">

15、 在TabBarItem中修正显示的颜色,当前点击项显示红色,其它项为默认颜色(黑色),在computed中增加isActive属性(实现活跃验证),将data()中的isActive删掉,然后运行测试:

isActive(){// === -1:表示当前路由中的路径不包含父组件传递过来的路径return this.$route.path.indexOf(this.path) !== -1
}

16、 继续提取出一个组件(MainTabBar.vue),放在components文件夹下的tabbar文件夹中,把App.vue文件内容复制到MainTabBar中,以便提取修改:

MainTabBar:

<template><div id="app"><tab-bar><!-- 根据需要在插槽位置插入4个子组件TabBarItem[数量可变] --><tab-bar-item path="/home" active-color="#ff5577"><template #item-icon><img src="../../assets/img/tabbar/home.svg" alt=""></template><template #item-icon-active><img src="../../assets/img/tabbar/home_active.svg" alt=""></template><template #item-text><div>首页</div></template></tab-bar-item><tab-bar-item path="/category" active-color="#ff5577"><template #item-icon><img src="../../assets/img/tabbar/category.svg" alt=""></template><template #item-icon-active><img src="../../assets/img/tabbar/category_active.svg" alt=""></template><template #item-text><div>分类</div></template></tab-bar-item><tab-bar-item path="/shopcart" active-color="#ff5577"><template #item-icon><img src="../../assets/img/tabbar/shopcart.svg" alt=""></template><template #item-icon-active><img src="../../assets/img/tabbar/shopcart_active.svg" alt=""></template><template #item-text><div>购物车</div></template></tab-bar-item><tab-bar-item path="/profile" active-color="#ff5577"><template #item-icon><img src="../../assets/img/tabbar/profile.svg" alt=""></template><template #item-icon-active><img src="../../assets/img/tabbar/profile_active.svg" alt=""></template><template #item-text><div>我的</div></template></tab-bar-item></tab-bar></div>
</template><script>
//导入TabBar组件
import TabBar from "./TabBar";
//导入TabBarItem组件
import TabBarItem from "./TabBarItem";
export default {name:'MainTabBar',components:{TabBarItem,TabBar}
}
</script><style>
.tab-bar-item img {width: 24px;height: 24px;margin-top: 3px;margin-bottom: 2px;vertical-align: middle;
}
</style>

App.vue:

<template><div id="app"><router-view/><main-tab-bar></main-tab-bar></div>
</template><script>
//导入MainTabBar组件
import MainTabBar from "@/components/tabbar/MainTabBar";
export default {name:'App',components:{MainTabBar}
}
</script><style>
@import "assets/css/base.css";
</style>

http://www.ppmy.cn/news/198249.html

相关文章

ThinkPad E580 U盘装机避坑

映像弄到U盘的我就不说了&#xff0c;网上教程一大堆&#xff1b; 映像弄好之后&#xff0c;开机F12发现怎么按都进不去&#xff1b; 然后就在网上找教程&#xff0c;发现网上的教程说把引导改成Legacy&#xff0c;结果就是这个原因&#xff0c;导致了上面的问题&#xff0c;…

联想TD350服务器主板型号,【新品】塔式机身 联想ThinkServer TD350

对于服务器类产品&#xff0c;联想一直都处在快速发展进步中&#xff0c;并且无论其推出的产品还是理念&#xff0c;都受到了很多用户的关注。今年9月22日&#xff0c;在“新云力量 联想企业级业务策略暨ThinkServer Gen5发布会”上&#xff0c;联想正式发布了基于最新的英特尔…

联想ThinkPad E40安装XP的方法

联想ThinkPad E40安装XP的方法 下面给大家记录的是E40安装XP的方法&#xff1a; 1、将SATA模式改成Compatibility 在正式安装XP以前&#xff0c;重启&#xff0c;按住F1进入BIOS&#xff0c;选择Config→SATA→Mode&#xff0c;由AHCI改成Compatibility&#xff0c;骗过XP安装程…

解决ThinkPad E580因AMD显卡导致系统崩溃的问题

前言 由于着急解决问题&#xff08;毕竟是常用电脑&#xff0c;而且现在也没法去修或者换电脑&#xff0c;修不好就真没用的了&#xff09;&#xff0c;所以基本全程没图&#xff0c;最后对问题原因有个猜测&#xff08;如题&#xff09;&#xff0c;可能是因为AMD显卡相关程序…

e580显卡驱动_联想ThinkPad E580笔记本8代CPU如何win10改win7

[文章导读]联想ThinkPad E580是一款15.6寸笔记本,其搭载intel 酷睿第八代处理器的笔记本。预装的是win10系统,用户还是喜欢win7系统,该笔记本采用的第八代酷睿CPU,在安装WIN7过程中USB设备不能使用,且intel 8代没有发布win7的核心显卡驱动,所以需要采用win7新机型安装,那…

Thinkpad E580 硬件错误0187、2200、2201解决经历

我的电脑是Thinkpad E580&#xff0c;最近电脑坏了&#xff0c;以下是具体情况&#xff1a; 一天中午&#xff0c;我打开电脑&#xff0c;在屏幕显示完联想的logn之后&#xff0c;它出现了——我从未见过的我的电脑出现这样的情况&#xff0c;它也给我带来了生活上的不便以及精…

e580显卡驱动_搭载AMD RX 550独显!联想Thinkpad E580评测:能玩大型游戏的亲民商务本...

一、前言&#xff1a;最低仅售5499元 更大屏幕的高配ThinkPad商务本E580 在大多数人的印象中&#xff0c;ThinkPad代表着坚固、沉稳、严谨、在设计上一丝不苟&#xff0c;所以在ThinkPad品牌诞生的20年时间内&#xff0c;其外形很少会有变化。ThinkPad E系列的推出&#xff0c;…

ThinkPad E580 U盘重装系统——制作U盘启动盘,系统重装

ThinkPad E580 U盘重装系统 之前自己捣鼓电脑&#xff0c;将原本的家庭版升级成了专业版&#xff0c;后面可能操作不当&#xff0c;电脑出现问题&#xff0c;有很多广告无故弹出&#xff0c;很多流氓软件删也删不掉&#xff0c;最后选择重装系统&#xff0c;重装回家庭版。 如果…