Encapsulate the navigation bar component with Vue

Encapsulate the navigation bar component with Vue

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:
  • Teach you how to create a project using vue-cli3 in five minutes (beginner's guide)
  • Some tips for using less in Vue projects
  • How to apply TypeScript classes in Vue projects
  • Vue.js implements tab switching and color change operation explanation
  • Detailed explanation of Vuex overall case
  • Click the toggle button in Vue to enable the button and then disable it
  • Vue component communication method case summary
  • Detailed explanation of the usage of scoped slots in Vue.js slots
  • Some suggestions on Vue code readability

<<:  Detailed explanation of Docker container cross-host multi-network segment communication solution

>>:  MySQL database query performance optimization strategy

Recommend

CocosCreator Getting Started Tutorial: Making Your First Game with TS

Table of contents premise TypeScript vs JavaScrip...

How to hide a certain text in HTML?

Text hiding code, hide a certain text in HTML Copy...

In-depth interpretation of /etc/fstab file in Linux system

Preface [root@localhost ~]# cat /etc/fstab # # /e...

React hooks pros and cons

Table of contents Preface advantage: shortcoming:...

In-depth discussion on auto-increment primary keys in MySQL

Table of contents Features Preservation strategy ...

Vue uses the method in the reference library with source code

The official source code of monaco-editor-vue is ...

Detailed steps for installing and using vmware esxi6.5

Table of contents Introduction Architecture Advan...

Detailed explanation of the wonderful uses of SUID, SGID and SBIT in Linux

Preface Linux's file permission management is...

MySQL database SELECT query expression analysis

A large part of data management is searching, and...

The practical process of login status management in the vuex project

Table of contents tool: Login scenario: practice:...

Introduction to Kubernetes (k8s)

I had always wanted to learn Kubernetes because i...