vue knowledge -TabBar achieve click on the corresponding icon realization Jump

New in src / components in a folder: tabbar

Vue create two files in the folder: TabBar.vue and TabBarItem.vue

1, in the TabBar.vue file: setting tab-bar style
  <template>

    <div id="tab-bar">

      <slot></slot>

    </div>

  </template>

 

  <script>

    export default{

      name:"TabBar"

    }

       </script>

  

  <style scoped>

#tab-bar {
display: flex;
background-color: #f6f6f6;

position: fixed;
left:0;
right:0;
bottom: 0;

box-shadow: 0 -1px 1px rgba(100,100,100,.1);
}

  </style>

  

2, TabBarItem.vue: set the style and other methods of each category

  Named Slot: when in use need to add a name

<template>

  <div class="tab-bar-item" @click="itemClick">

    <div v-if="!isActive">  

      <slot name="item-icon"></slot>

    </div>

    <div v-else>

      <slot name="item-icon-active"></slot>

    </div>

    <div :class="{active:isActive}">

      <slot name="item-text"></slot>

    </div>

  </div>

</template>

<script>

  export default{

    name:"TabBarItem",

    props:{

      path:String,

    },

    data(){

      return {

        isActive:true

      }

    },

    methods:{

      itemClick(){

        this.$router.push(this.path)

      }

    }

  }

</script>

<style scoped>

.tab-bar-item{
flex:1;
text-align: center;
height:49px;
}
.tab-bar-item img{
margin-top:3px;
width:18px;
height:18px;
vertical-align: middle;
margin-bottom: 2px;
}
.active{
color: red;
}

</style>

 

3, cited in App.vue in:

  <template>

    <div id="app">  

      <router-view></router-view>

<tab-bar>
<tab-bar-item path="/home">
<img slot='item-icon' src="./assets/img/tabbar/首页.svg" alt="">
<img slot='item-icon-active' src="./assets/img/tabbar/home_active.svg" alt="">
<div slot="item-text">首页</div>
</tab-bar-item>
<tab-bar-item path="/category">
<img slot='item-icon' src="./assets/img/tabbar/分类.svg" alt="">
<img slot='item-icon-active' src="./assets/img/tabbar/goods_active.svg" alt="">
<div slot="item-text">分类</div>
</tab-bar-item>
<tab-bar-item path="/cart">
<img slot='item-icon' src="./assets/img/tabbar/购物车空.svg" alt="">
<img slot='item-icon-active' src="./assets/img/tabbar/shopcart_active.svg" alt="">
<div slot="item-text">购物车</div>
</tab-bar-item>
<tab-bar-item path="/profile">
<img slot='item-icon' src="./assets/img/tabbar/我的.svg" alt="">
<img slot='item-icon-active' src="./assets/img/tabbar/mine_active.svg" alt="">
<div slot="item-text">我的</div>
</tab-bar-item>
</tab-bar>

         </div>

  </template>

  

  <script>

    import TabBar from './compnents/tabbar/TabBar.vue'

    import TabBarItem from './components/tabbar/TabBarItem.vue'

    export default {

      name:"App",

      components: {  

        Tabs,

        TabBarItem

      }

    }

  </script>

 

  <style>

    @import url('./assets/css/base.css')

  </style>

Guess you like

Origin www.cnblogs.com/jjbw/p/12129480.html