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

Installation steps of mysql under linux

1. Download the mysql tar file: https://dev.mysql...

Detailed explanation of whereis example to find a specific program in Linux

Linux finds a specific program where is The where...

Use html-webpack-plugin' to generate HTML page plugin in memory

When we package the webpackjs file, we introduce ...

Detailed explanation of firewall rule settings and commands (whitelist settings)

1. Set firewall rules Example 1: Expose port 8080...

Optimizing query speed of MySQL with tens of millions of data using indexes

1. The role of index Generally speaking, an index...

Example of implementing GitHub's third-party authorization method in Vue

Table of contents Creating OAuth Apps Get the cod...

Vue must learn knowledge points: the use of forEach()

Preface In front-end development, we often encoun...

Vue implements login jump

This article example shares the specific code of ...

20 CSS coding tips to make you more efficient (sorted)

In this article, we would like to share with you ...

MySQL-8.0.26 Configuration Graphics Tutorial

Preface: Recently, the company project changed th...

Keepalived implements Nginx load balancing and high availability sample code

Chapter 1: Introduction to keepalived The purpose...

How to implement the strategy pattern in Javascript

Table of contents Overview Code Implementation Su...