为了账号安全,请及时绑定邮箱和手机立即绑定

Vue2.0 路由配置及Tab组件开发

标签:
Vue.js

webp

Unsplash

本次的系列博文的知识点讲解和代码,主要是来自于 黄轶 在慕课网的  Vue 2.0 高级实战-开发移动端音乐WebApp 课程,由个人总结并编写,其代码及知识点部分,均有所更改和删减,关于更多 Vue 2.0 的知识和实际应用,还请大家购买课程进行学习实践,该系列博文的发布已得到黄轶老师的授权许可

webp

授权许可

0 系列文章目录

Vue2.0 定制一款属于自己的音乐 WebApp
Vue2.0 路由配置及Tab组件开发
Vue2.0 数据抓取及Swiper组件开发
Vue2.0 scroll 组件的抽象和应用
Vue2.0 歌手数据获取及排序
Vue2.0 歌手列表滚动及右侧快速入口实现
Vue2.0 Vuex初始化及歌手数据的配置

1. 路由配置

我们在上一章节中完成了 header 组件的开发,并预先编写好了顶部栏,排行,推荐,搜索,歌手页面,在传统的 Web 开发中,页面之间的切换是通过超链接进行跳转的,而 Vue 的单页面应用是基于路由和组件的,路由用于设定访问路径,并将路径和组件映射起来,所以 Vue 的页面跳转实际上就是组件的切换

现在我们在 Router 中 import 定义好的组件,并且引入到 Vue 实例当中

// router/index.jsimport Vue from 'vue'import Router from 'vue-router'import Reacommend from 'components/recommend/recommend'import Singer from 'components/singer/singer'import Rank from 'components/rank/rank'import Search from 'components/search/search'Vue.use(Router)export default new Router({  routes: [
    {      path: '/recommend',      component: Reacommend
    },
    {      path: '/singer',      component: Singer
    },
    {      path: '/rank',      component: Rank
    },
    {      path: '/search',      component: Search
    }
  ]
})
// main.jsimport 'babel-polyfill'import Vue from 'vue'import App from './App'import router from './router'import fastclick from 'fastclick'import './common/stylus/index.styl'fastclick.attach(document.body)new Vue({  el: '#app',  render: h => h(App),
  router
})

不要忘记在需要渲染该组件的地方,挖坑 <router-view>,即在该节点渲染路径匹配到的视图组件

// App.vue<template>  <div id="app">
    <m-header></m-header>
    <router-view></router-view>
  </div></template><script type="text/ecmascript-6">
    import MHeader from 'components/m-header/m-header'
    
    export default {        components: {
            MHeader
        }
    }</script><style scoped lang="stylus" rel="stylesheet/stylus"></style>

2. Tab 组件

路由配置完成后,我们接下来需要开发 Tab 组件,用以进行路由的切换,该组件的结构及样式如下

// components/tab/tab.vue<template>
  <div class="tab">
    <router-link tag="div" class="tab-item" to="/recommend">
      <span class="tab-link">推荐</span>
    </router-link>
    <router-link tag="div" class="tab-item" to="/singer">
      <span class="tab-link">歌手</span>
    </router-link>
    <router-link tag="div" class="tab-item" to="/rank">
      <span class="tab-link">排行      </span>
    </router-link>
    <router-link tag="div" class="tab-item" to="/search">
      <span class="tab-link">搜索</span>
    </router-link>
  </div></template><script type="text/ecmascript-6">
  export default {}</script><style scoped lang="stylus" rel="stylesheet/stylus">
  @import "~common/stylus/variable"
  .tab
    display: flex
    height: 44px
    line-height: 44px
    font-size: $font-size-medium
    .tab-item
      flex: 1
      text-align: center
      .tab-link
        padding-bottom: 5px
        color: $color-text-l
      &.router-link-active
        .tab-link
          color: $color-theme
          border-bottom: 2px solid $color-theme</style>

我们在其中嵌套了很多 <router-link> 标签,tag 属性用以渲染成某种标签,渲染后该标签仍会监听点击,触发导航;to 属性为目标路由的链接,&.router-link-active 为当前路由,可以添加 active 样式

同样的,需要要在 App.vue 中引入并输出 Tab 组件,并且项目启动时,我们为其配置一个默认跳转的路由

// router/index.js...
export default new Router({  routes: [
    {      path: '/',      redirect: '/recommend'
    },
    ...
  ]
})

webp

运行结果

End of File



作者:Nian糕
链接:https://www.jianshu.com/p/9ea0935421c5


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消