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

How to build a K8S cluster and install docker under Hyper-V

If you have installed the Win10 system and want t...

How to use .htaccess to prohibit a certain IP from accessing the website

Preface For cost considerations, most webmasters ...

How to count the number of specific characters in a file in Linux

Counting the number of a string in a file is actu...

How to use mixins in Vue

Table of contents Preface How to use Summarize Pr...

How to encapsulate axios request with vue

In fact, it is very simple to encapsulate axios i...

Why web page encoding uses utf-8 instead of gbk or gb2312?

If you have a choice, you should use UTF-8 In fac...

Three ways to configure JNDI data source in Tomcat

In my past work, the development server was gener...

Vue uses canvas to realize image compression upload

This article shares the specific code of Vue usin...

Unzipped version of MYSQL installation and encountered errors and solutions

1 Installation Download the corresponding unzippe...

What is the function and writing order of the a tag pseudo class

The role of the a tag pseudo-class: ":link&qu...

Vue project implements left swipe delete function (complete code)

Achieve results The code is as follows html <t...

Configuring MySQL and Squel Pro on Mac

In response to the popularity of nodejs, we have ...

User needs lead to marketing-oriented design

<br />For each of our topics, the team will ...