Preface: Fully encapsulating a functional module using component-based ideas, such as a navigation bar, has many benefits for both our development ideas and efficiency. In development, we should try to use component-based development ideas as much as possible, and do not write all the code in the same .vue file, which can greatly improve the readability of the code. Encapsulating the navigation bar The main idea is to treat the red parts as components, and they are just different in pictures and texts, so we can encapsulate them into the same component, and then pass the picture information and text information into the component (slots can be used). //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> The next step is to encapsulate a container that puts these four options in the same place. //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> The next step is to use it and write different pictures and text information to each different TabBarItem slot. //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">Home</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">Category</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">Shopping Cart</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">My</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> The navigation bar is generally used in the home page, so we put this navigation bar in 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 } } Summary: It seems that we use 3 files to write a navigation bar, which may seem troublesome, but it also greatly improves the readability of the code. If we still need to use the navigation bar elsewhere in the project, we only need to create a file similar to MainTabBar, and then write the pictures and texts you want into it. Even when used in other projects, we can directly copy the file and use it directly. We don’t even need to write CSS styles, which greatly improves our development efficiency. The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Detailed explanation of Docker container cross-host multi-network segment communication solution
>>: MySQL database query performance optimization strategy
We all know that the performance of applications ...
Table of contents premise TypeScript vs JavaScrip...
It is not possible to use width and height directl...
Text hiding code, hide a certain text in HTML Copy...
Preface [root@localhost ~]# cat /etc/fstab # # /e...
Background - Online Alert An online server issued...
Table of contents Preface advantage: shortcoming:...
Table of contents Features Preservation strategy ...
1. Replication Principle The master server writes...
The official source code of monaco-editor-vue is ...
Table of contents Introduction Architecture Advan...
Preface Linux's file permission management is...
A large part of data management is searching, and...
Table of contents tool: Login scenario: practice:...
I had always wanted to learn Kubernetes because i...