项目之头部选项卡

做出效果如下


微信截图_20181109005128.png

我们需要用到一个第三方插件

npm i ly-tab -S

因为这个插件是在全局中使用的,因此需要在全局的main.js文件下引入

import LyTab from 'ly-tab'
Vue.use(LyTab);

因为只有在Home这个页面上才有用到,所以我们在Home这个路由上配置即可

<template>
    <div class="home">
        <ly-tab
         v-model="selectedId"
         :items="items"
         :options="options"
         

         class="fix"
        />
        <router-view></router-view>
    </div>
</template>

<script>

    export default{
        name:"Home",
        data(){
            return{
                selectedId:0,
                items:[
                    {label:'热门'},
                    {label:'服饰'},
                    {label:'鞋包'},
                    {label:'母婴'},
                    {label:'百货'},
                    {label:'食品'},
                    {label:'内衣'},
                    {label:'男装'},
                    {label:'电器'}
                ],
                options:{
                    activeColor:'#e9232c',
                },
                subRouteUrl:['/home/hot','/home/dress','/home/box',
                '/home/mbaby','/home/general','/home/food','/home/shirt',
                '/home/man','/home/ele']
            }
        },
        methods:{
            handleChange(item,index){
                this.$router.replace(this.subRouteUrl[index]);
            }
        }
    }


</script>

<style scoped lang="stylus" ref="stylesheet/stylus">
.home
    background #f5f5f5
    width 100%
    height 100%

    .fix
        position fixed
        left 0
        top 0
        z-index 998
</style>

因为存在2级路由关系,所以在home路由下创建以下子路由


微信截图_20181109011713.png

然后在路由配置项中进行配置

import Hot from './../pages/Home/Children/Hot/Hot'
import Dress from './../pages/Home/Children/Dress'
import Ele from './../pages/Home/Children/Ele'
import Food from './../pages/Home/Children/Food'
import General from './../pages/Home/Children/General'
import Box from './../pages/Home/Children/Box'
import Man from './../pages/Home/Children/Man'
import Mbaby from './../pages/Home/Children/Mbaby'
import Shirt from './../pages/Home/Children/Shirt'

export default new VueRouter({
    //3.1 配置一级路由
    routes:[
        {
            path:'/home',
            component:Home,
            children:[
                {path:'hot',component:Hot},
                {path:'box',component:Box},
                {path:'dress',component:Dress},
                {path:'ele',component:Ele},
                {path:'food',component:Food},
                {path:'general',component:General},
                {path:'man',component:Man},
                {path:'shirt',component:Shirt},
                {path:'mbaby',component:Mbaby},
                {path:'/home',redirect:'/home/hot'}
            ]
        },

解释代码难点部分

1.ly-tab这个上面各种看不懂的东西,都是组件自己要求配置的选项,比如v-model:'selectedID',就是组件规定选中的哪个地方的东西,0就是默认选中第一个,option配置的是选中时字体的颜色
2.@change="handleChange" @change是组件规定的方法,后面方法名字自己定义即可,方法默认自带两个参数,一个item是选中的项,一个index是选中的索引

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,126评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,156评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,993评论 6 342
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 13,086评论 2 59
  • 一天,小蚂蚁正正在森林散步。突然,大象壮壮闯过来,挡住了蚂蚁正正的路。 蚂蚁正正说:“大块头,你没事找事呀,我告诉...
    无敌小品子阅读 916评论 5 44