vue 实现底部固定 tab 导航


方法一:通过$route.path判断跳转

<template>
  <ul class='end-menu flex-row'>
    <li
      v-for='(item, index) in menulist'
      :key='index'
      @click='changeMenu(item.path)'
      :class="[$route.path === item.path ? 'active' : '']"
    >
      <img :src='$route.path === item.path ? item.active : item.normal' alt=''>
      <span>{{item.title}}</span>
    </li>
  </ul>
</template>


<script>
export default {
  name: 'endMenu',
  data () {
    return {
      // 直接粘贴路由表过来
      menulist: [
        {
          path: '/home',
          normal: require('@/assets/img/logo.png'),
          active: require('@/assets/img/logo.png'),
          title: '首页'
        },
        {
          path: '/living',
          normal: require('@/assets/img/logo.png'),
          active: require('@/assets/img/logo.png'),
          title: '直播'
        },
        {
          path: '/goodsblock',
          normal: require('@/assets/img/logo.png'),
          active: require('@/assets/img/logo.png'),
          title: '好物'
        },
        {
          path: '/carlist',
          normal: require('@/assets/img/logo.png'),
          active: require('@/assets/img/logo.png'),
          title: '购物车'
        },
        {
          path: '/mine',
          normal: require('@/assets/img/logo.png'),
          active: require('@/assets/img/logo.png'),
          title: '我的'
        }
      ]
    }
  },
  methods: {
    changeMenu (path) {
      if (this.$route.path === path) { return }
      this.$router.replace(path)
    }
  }
}
</script>

 

方法二:通过router-link跳转

<template>
  <div class='end-menu flex-row'>
    <router-link
      v-for='(item, index) in menulist'
      :key='index'
      :to='item.path'
      active-class='active'
    >
      <img :src='active === index ? item.active : item.normal' alt=''>
      <span>{{item.title}}</span>
    </router-link>
  </div>
</template>


<script>
export default {
  name: 'endMenu',
  data () {
    return {
      menulist: [
        {
          path: '/home',
          normal: require('@/assets/img/logo.png'),
          active: require('@/assets/img/logo.png'),
          title: '首页'
        },
        {
          path: '/living',
          normal: require('@/assets/img/logo.png'),
          active: require('@/assets/img/logo.png'),
          title: '直播'
        },
        {
          path: '/goodsblock',
          normal: require('@/assets/img/logo.png'),
          active: require('@/assets/img/logo.png'),
          title: '好物'
        },
        {
          path: '/carlist',
          normal: require('@/assets/img/logo.png'),
          active: require('@/assets/img/logo.png'),
          title: '购物车'
        },
        {
          path: '/mine',
          normal: require('@/assets/img/logo.png'),
          active: require('@/assets/img/logo.png'),
          title: '我的'
        }
      ]
    }
  }
}
</script>

声明:Jensonhui's blog|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - vue 实现底部固定 tab 导航


Just do it.