脚手架2搭建Tabbar

components文件夹下添加tabbar文件夹,

然后添加文件TabBar.vue和TabBarItem.vue

<template>
  <div id="tab-bar">
      <slot></slot>
  </div>
</template>

<script>


export default {
  name: "TabBar"
 
};
</script>

<style>
#tab-bar {
  display: flex;
  background-color: #f6f6f6;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  box-shadow: 0 -1px 1px rgb(100, 100, 100, 0.2);
}

</style>
<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 :style="activeStyle">
      <slot name="item-text"></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: "TabBarItem",
  props:{
      path:String,
      activeColor:{
        type:String,
        default:'red'
      }
  },
  data() {
    return {

    };
  },
  computed:{
     isActive() {
       return this.$route.path.indexOf(this.path)!= -1;
     },
     activeStyle() {
       return this.isActive ? {color:this.activeColor} : {}
     }
  },
  methods:{
      itemClick() {
          this.$router.replace(this.path) ;
      }
  }
};
</script>

<style scoped>
.tab-bar-item {
  flex: 1;
  text-align: center;
  height: 49px;
  font-size: 14px;
}

.tab-bar-item img {
  width: 24px;
  height: 24px;
  margin-top: 3px;
  vertical-align: middle;
  margin-bottom: 2px;
}


</style>

components添加文件MainTabBar.vue

<template>
  <tab-bar>
    <tab-bar-item path="/home">
      <img slot="item-icon" src="../assets/img/tabbar/huo2.png" alt />
      <img slot="item-icon-active" src="../assets/img/tabbar/huo3.png" alt />
      <div slot="item-text">首页</div>
    </tab-bar-item>
    <tab-bar-item path="/category">
      <img slot="item-icon" src="../assets/img/tabbar/huo2.png" alt />
      <img slot="item-icon-active" src="../assets/img/tabbar/huo3.png" alt />
      <div slot="item-text">分类</div>
    </tab-bar-item>
    <tab-bar-item path="/cart">
      <img slot="item-icon" src="../assets/img/tabbar/huo2.png" alt />
      <img slot="item-icon-active" src="../assets/img/tabbar/huo3.png" alt />
      <div slot="item-text">购物车</div>
    </tab-bar-item>
    <tab-bar-item path="/profile">
      <img slot="item-icon" src="../assets/img/tabbar/huo2.png" alt />
      <img slot="item-icon-active" src="../assets/img/tabbar/huo3.png" alt />
      <div slot="item-text">我的</div>
    </tab-bar-item>
  </tab-bar>
</template>


<script>
import TabBar from "../components/tabbar/TabBar";
import TabBarItem from "../components/tabbar/TabBarItem";

export default {
  name: "MainTabBar",
  components: {
    TabBar,
    TabBarItem
  }
};
</script>
<style scoped>
</style>

router文件夹下添加index.js

import Vue from 'vue'
import Router from 'vue-router'

const Home = () => import('../views/home/Home')
const Category = () => import('../views/category/Category')
const Cart = () => import('../views/cart/Cart')
const Profile = () => import('../views/profile/Profile')

Vue.use(Router)
// 创建路由对象
const routes = [
  {
    path: '/home',
    component: Home
  }, {
    path: '/category',
    component: Category
  }, {
    path: '/cart',
    component: Cart
  }, {
    path: '/profile',
    component: Profile
  },
];

const router = new Router({
  routes,
  mode:'history'
})

// 导出router
export default router

App.Vue

<template>
  <div id="app">
    <router-view></router-view>
    <main-tab-bar></main-tab-bar>
  </div>
</template>

<script>

import MainTabBar from "./components/MainTabBar";

export default {
  name: "App",
  components: {

    MainTabBar
  }
};
</script>

<style>
@import "./assets/css/base.css";
</style>

main.js

import Vue from 'vue'
import App from './App'
import router from './router'

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  render: h => h(App)
})

 

posted @ 2020-04-20 21:14  bradleydan  阅读(108)  评论(0)    收藏  举报