vue2 二级路由 高亮

实现效果图

《vue2 二级路由 高亮》

1、项目中的图标使用的是element-ui框架中的图标,如果需要引入可以看我写的上一篇文章。
2、首先配置路由
我初始化项目的时候初始化了路由,所以打开router/index.js文件进行修改配置

router/index.js

import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/components/Home'
import Game from '@/components/Game'
import Bbs from '@/components/Bbs'
import Me from '@/components/Me'
import Nba from '@/components/Nba'
import Recommend from '@/components/Recommend'

Vue.use(Router)

export default new Router({
  mode: 'history',
  linkActiveClass: 'active',
  routes: [
    { path: '/', redirect: '/home' }, // 重定向到 home
    {
      path: '/home',
      name: 'Home',
      component: Home,
      // children path中"/home/"可以省略
      children: [
        {
          path: '/',                  // 子路由重定向
          redirect: 'recommend'
        },
        {
          path: 'recommend',
          name: 'recommend',
          component: Recommend
        },
        {
          path: 'nba',
          name: 'nba',
          component: Nba
        },
        {
          path: 'video',
          name: 'video',
          component: Nba
        },
        {
          path: 'entertain',
          name: 'entertain',
          component: Nba
        }
      ]
    },
    {
      path: '/game',
      name: 'Game',
      component: Game
    }, {
      path: '/bbs',
      name: 'Bbs',
      component: Bbs
    }, {
      path: '/me',
      name: 'Me',
      component: Me
    }
  ]
})

app.vue
底部导航封装为TabBar组件,在app.vue中引入

<template>
  <div id="app">
    <div :class="{router: true}">
      <router-view/>
    </div>
    <!-- 底部导航组件 -->
    <div :class="{tabbar: true}">
      <tab-bar></tab-bar>
    </div>
  </div>
</template>

<script>
import TabBar from './components/Tabs'
export default {
  name: 'App',
  components: {
    // 底部导航组件
    TabBar
  }
}
</script>

<style scoped>
  #app {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
  }
  .router {
    flex: 1;
    padding: 10pt;
  }
  .tabbar {
    height: 30pt;
    padding: 10pt 0;
    border-top: 1pt solid #e6e6e6;
    background: #fbfbfb;
  }
</style>

Tabs.vue

<template>
  <div id="tabs">
    <div class="home">
      <!-- 点击其他tab页,再次点击home的时候,路由重定向到了recommend,注意写法 to="/home/" -->
      <router-link to="/home/" tag="div">
          <div><i class="el-icon-s-home"></i></div>
        <div>首页</div>
      </router-link>
    </div>
    <div class="game">
      <router-link :to="{name: 'Game'}" tag="div">
          <div><i class="el-icon-s-goods"></i></div>
        <div>比赛</div>
      </router-link>
    </div>
    <div class="bbs">
      <router-link :to="{name: 'Bbs'}" tag="div">
          <div><i class="el-icon-share"></i></div>
        <div>社区</div>
      </router-link>
    </div>
    <div class="me">
      <router-link :to="{name: 'Me'}" tag="div">
          <div><i class="el-icon-s-custom"></i></div>
        <div>我</div>
      </router-link>
    </div>
  </div>
</template>
<script>
export default {
  name: 'TabBar'
}
</script>
<style scoped>
    #tabs {
        width: 100%;
        display: flex;
        flex-direction: row;
        justify-content: space-around;
        align-items: center;
        text-align: center;
        color: #b5b5b5;
    }
    #tabs i {
        font-size: 18pt;
    }
    .active {
        color: #468dcc;
    }
</style>

这样就添加了底部导航,然后我们配置home界面,home界面中有二级导航,而且在首页的二级导航选中的时候,需要高亮显示”首页“tab页

Home.vue

<template>
  <div id="home">
    <div :class="{topbar: true}">
      <router-link :to="{name: 'recommend'}" tag="div">推荐</router-link>
      <router-link :to="{name: 'nba'}" tag="div">篮球(NBA)</router-link>
      <router-link :to="{name: 'video'}" tag="div">视频</router-link>
      <router-link :to="{name: 'entertain'}" tag="div">影视娱乐</router-link>
    </div>
    <div :class="{tabInfo: true}">
      <router-view/>
    </div>
  </div>
</template>
<script>
export default {
  name: 'Home',
  data () {
    return {
      name: 'home'
    }
  }
}
</script>
<style scoped>
  #home {
    display: flex;
    flex-direction: column;
    text-align: left;
    height: 100%;
  }
  .topbar {
    height: 26pt;
    font-size: 12pt;
    color: #343434;
    background: #fbfbfb;
    border-bottom: 1pt solid #e6e6e6;
    margin-bottom: 10pt;
    display: flex;
    flex-direction: row;
  }
  .topbar div {
    margin: 0 5pt;
  }
  .topbar span {
    padding-bottom: 11pt;
  }
  .active {
    color: #468dcc;
    border-bottom: 1pt solid #468dcc;
    font-weight: bold;
  }
  .tabInfo {
    flex: 1;
  }
</style>

路由传参

<template>
  <div>
    <div v-for="(item, index) of newsList" :key="index">
      <!-- 传递params参数和query查询参数 -->
      <router-link :to="{name: 'detail', params: {id: 1, item: item}, query: {name: 'zxc'}}">{{item.name}}</router-link>
    </div>
    <div @click="pushParams()">传递参数</div>
  </div>
</template>
<script>
export default {
  name: 'Nba',
  data () {
    return {
      name: 'Nba',
      newsList: [
        {name: '杜兰特G5出战城疑'},
        {name: '消息人士称放走拉塞尔对于蓝网而言是很难的做法'}
      ],
      pushParams () {
        this.$router.push({name: 'detail', params: {id: 1, item: {name: "杜兰特复出勇士大胜猛龙"}}, query: {name: 'zxc'}})
      }
    }
  }
}
</script>
<style scoped>

</style>
<template>
  <div>
    <!-- params参数 -->
    {{$route.params.item.name}}
    <!-- 查询参数 query -->
    {{$route.query.name}}
    <div @click="goback()">返回</div>
  </div>
</template>
// this.$route.query 或者 this.$route.params
<script>
console.log(this.route)
export default {
  name: 'NbaDetail',
  data () {
    return {
      goback () {
        this.$router.go(-1)
      }
    }
  },
  watch: {
    '$route' (to, from) {
      console.log(to, from)
    }
  }
}
</script>
<style scoped>

</style>

push append replace

    原文作者:zxc19890923
    原文地址: https://segmentfault.com/a/1190000019433786
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞