vue2 中二级路由高亮问题及配置方法

2020-06-14 06:28:19易采站长站整理

实现效果图

 

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/" -->