前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Vue2.0 路由配置及Tab组件开发

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

作者头像
Nian糕
修改2024-03-16 17:24:04
4760
修改2024-03-16 17:24:04
举报

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

授权许可
授权许可

0 系列文章目录

Vue2.0 定制一款属于自己的音乐 WebApp

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

Vue2.0 数据抓取及Swiper组件开发

Vue2.0 scroll 组件的抽象和应用

Vue2.0 歌手数据获取及排序

Vue2.0 歌手列表滚动及右侧快速入口实现

1. 路由配置

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

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

代码语言:javascript
复制
// router/index.js

import 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
    }
  ]
})
代码语言:javascript
复制
// main.js

import '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>,即在该节点渲染路径匹配到的视图组件

代码语言:javascript
复制
// 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组件,用以进行路由的切换,该组件的结构及样式如下

代码语言:javascript
复制
// 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组件,并且项目启动时,我们为其配置一个默认跳转的路由

代码语言:javascript
复制
// router/index.js

...
export default new Router({
  routes: [
    {
      path: '/',
      redirect: '/recommend'
    },
    ...
  ]
})
运行结果
运行结果

该章节的内容到这里就全部结束了,源码我已经发到了 GitHub Vue_Music_02 上了,有需要的同学可自行下载

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.07.17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 0 系列文章目录
  • 1. 路由配置
  • 2. Tab 组件
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档