Vue - tabbar和路由结合效果


一、创建路由

import VueRouter from 'vue-router'
import Vue from 'vue'

// 1.安装插件
Vue.use(VueRouter)

const routes = [
]

// 2.创建路由
const router = new VueRouter({
  routes
})

export default router

二、配置组件

创建一个views文件夹, 用来存放组件.
然后针对每个功能创建一个文件夹
在这里插入图片描述

每个文件夹里面只存放各自的功能, 公共的部分放在components里面

三、让使用者传递path

TabBarItem.vue

<template>
  <div class="tab-bar-item" @click="textCilck">
    <slot v-if="isActive" name="item-icon"></slot>
    <slot v-else name="item-icon-active"></slot>
    <div :class="{active: !isActive}"><slot name="item-text"></slot></div>
  </div>
  
</template>

<script>
  export default {
    name: 'TabBarItem',
    props: {
      path: String,
    },
    data() {
      return {
        // isActive: isActived
      }
    },
    methods: {
      textCilck() {
        this.$router.push(this.path)
      }
    },
    computed: {
      isActive() {
        // 判断当前活跃的path是否和传递过来的path相同
        return this.$route.path.indexOf(this.path) == -1
      }
    }
  }
</script>

<style>
  .tab-bar-item {
    flex: 1;
    text-align: center;
    height: 49px;
    font-size: 14px;
  }
  .tab-bar-item img {
    height: 24px;
    width: 24px;
  }
  .active {
    color: red;
  }
</style>

四、动态决定点击后的颜色

<template>
  <div class="tab-bar-item" @click="textCilck">
    <slot v-if="!isActive" name="item-icon"></slot>
    <slot v-else name="item-icon-active"></slot>
    <!-- 动态绑定style -->
    <div :style="activeStyle"><slot name="item-text"></slot></div>
  </div>
  
</template>

<script>
  export default {
    name: 'TabBarItem',
    props: {
      // 从父元素里面接收一个path
      path: String,
      // 从父元素里面接受一个color
      activeColor: {
        type: String,
        default: 'red'
      }
    },
    data() {
      return {
        // isActive: isActived
      }
    },
    methods: {
      textCilck() {
        this.$router.push(this.path)
      }
    },
    computed: {
      isActive() {
        // 判断当前活跃的path是否和传递过来的path相同
        return this.$route.path.indexOf(this.path) !== -1
      },
      // 动态绑定style
      activeStyle() {
        return this.isActive ? {color: this.activeColor} : {}
      }
    }
  }
</script>

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

五、路径别名

webpack里面配置了一个路径别名 : 即 src 可以用 @ 表示

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值