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

vue实用技巧之路由跳转

标签:
Vue.js

写过的代码,踩过的坑,终将成为解决技术问题的宝贵经验

如果你因为路由跳转刷新问题而烦恼,或者是和这篇文章https://blog.csdn.net/wang1006008051/article/details/78686451讲的差不多(该文章的解决方案有bug+ _ +),那我的方法便能很好解决你的问题

webp

image.png


需求:实现路由跳转,选中状态

webp

image.png

方案1.0.0

1、使用router-link跳转,会自带class名,改变其样式即可


webp

image.png


2、代码

// html<ul>    <li v-for="(item,index) of navList" @click="selectNav(item.id)">
    <router-link :to="'/'+item.id">{{item.title}}</router-link>
   </li></ul>// css.router-link-exact-active{        color: red

    }// js
    data(){        return {            navList:[
            {title: '首页',id:'index',}, 
            {title: '店铺',id:'shop'}, 
            {title: '创业直播',id:'live'}, 
            {title: '我的',id:'my'}
            ]

        }
    },// router.js{      path: '/index', 
      name: '首页',// 路由项的名字
      component: Home // 组件的名字
    },
      {      path: '/shop', 
      name: '店铺',      component: Home 
    },
      {      path: '/live', 
      name: '创业直播',      component: Home ,

    },
      {      path: '/my', 
      name: '我的',      component: Home 
    },

3、缺点,只适合一级导航或菜单跳转,当有二级跳转,便会有bug


webp

image.png

我们想要的效果是页面是子页面,但选中的状态是父级


webp

image.png

方案1.0.1

1、设置一个状态,判断时候和当前相等,相等则选中

data(){        return {            // 设置状态
            isSelect:"",
            navList:[
            {title: '首页',id:'index',}, 
            {title: '店铺',id:'shop'}, 
            {title: '创业直播',id:'live'}, 
            {title: '我的',id:'my'}
            ]

        }
    },

2、循环navList,执行点击事件,进行路由跳转

// html
<ul>
            <li v-for="(item,index) of navList" @click="selectNav(item.id)">
                <p :class="isSelect == item.title ? 'active' : ''">{{item.title}}</p>
            </li>
            
        </ul>
        <router-link :to="'/live/'+id">我是产业创业直播的子页面</router-link>
            <router-link :to="'/my/'+id">我的子页面</router-link>
// js
selectNav (id) {
          this.$router.push({path: `/${id}`});
            this.isSelect = this.$route.name
  
      }
// router.js
  {
      path: '/index', 
      name: '首页',// 路由项的名字
      component: Home // 组件的名字
    },
      {
      path: '/shop', 
      name: '店铺',
      component: Home 
    },
      {
      path: '/live', 
      name: '创业直播',
      component: Home ,

    },
      {
      path: '/my', 
      name: '我的',
      component: Home 
    },
      {
      path: '/live/:id', 
      name: 'ss',
      component: Home 
    },
     {
      path: '/my/:id', 
      name: '我的1',
      component: Home 
    },

3、利用$routes自带属性进行判断,是否和当前页一样

// 此方法可解决刷新时候选中的状态
    mounted(){        this.isSelect = this.$route.name;        if(this.$route.name === "default"){            this.isSelect = "首页"
        }else if(/^\/live/g.test(this.$route.path)){            console.log(2)                this.isSelect = "创业直播"
        }else if(/^\/my/g.test(this.$route.path)){            this.isSelect = "我的"
        }
   
    },// 此方法是点击选中的是否选中的状态
    created(){    
this.$router.afterEach(to => {      
    if(/^\/live/g.test(to.path)){        this.isSelect = "创业直播"
    }else if(/^\/my/g.test(to.path)){        this.isSelect = "我的"
    }
    });
    }

}

代码或许冗余,如果你们有更好的解决方案,欢迎指点。。。



作者:jia林
链接:https://www.jianshu.com/p/7a9ea8dbbd30


点击查看更多内容
TA 点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消