Vue实现头部导航

  1. views文件下创建 Layout.vue ,Mine.vue, Search.vue, Topic.vue组件

  2. 配置路由,Layout组件是入口文件,其他组件作为Layout组件的子组件

  3. 配置点击高亮–> linkActiveClass : “active”,

const router = new VueRouter({

  mode: 'history',
  base: process.env.BASE_URL,
  linkActiveClass: "active",   //配置点击高亮
  routes: [{
      path: "/",
      name: "Layout",
      component: Layout,
      children: [{
          path: '/',
          name: 'Home',
          component: Home
        },
        {
          path: 'mine',
          name: 'mine',
          component: Mine
        },
        {
          path: 'search',
          name: 'search',
          component: Search
        },
        {
          path: 'topic',
          name: 'topic',
          component: Topic
        }
      ]
    }
  ]
  
})
  1. App.vue 中只留一个路由出口,App内的样式全部删除
<template>
             <div id="app">
    	      <router-view></router-view>
              </div>
</template>
  1. 配置Layout组件显示路由
<template>
    <div>
        <ul class="top-nav">
            <li>
                <router-link exact to="/">首页</router-link> 
            </li>
            <li>
                <router-link to="/mine">我的</router-link>
            </li>
            <li>
                <router-link :to="{name:'search'}">搜索</router-link> 
            </li>
            <li>
                <router-link :to="{name:'topic'}">榜单</router-link> 
            </li>
        </ul>

        <router-view/>
    </div>
</template>
<script>
export default {
    name:"Layout",
}
</script>
<style lang="less" scoped>
.top-nav {
    display: flex;
    background: #fff;
    li {
        list-style: none;
        flex: 1;
        text-align: center;
        padding-top: 10px;
        a {
            text-decoration: none;
            display: block;
            padding-bottom: 8px;
        }
        .active {
            border-bottom: 2px solid #ff0000;
        }
    }
}
</style>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值