antd-vue a-menu菜单绑定路由相关问题

antd-vue a-menu菜单绑定路由相关问题

tips: 路由绑定、菜单跳转、网页后退高亮显示

1. 问题描述

使用antd-vue 的 a-layout布局和a-menu菜单做一个侧边栏菜单,加入vuex配置侧边栏点击事件,实现点击菜单改变路由展示中间部分内容的功能

但是出现了问题:

  • 重复点击路由报错

  • 浏览器刷新/后退 菜单高亮区域没有根据路由的变化产生变化

2. 解决方法

  1. 对路由变化进行判断/修改router 的push与replace方法

  2. 借助a-menu 的属性::selectedKeys绑定路由地址,就能实现随着路由产生变化

3. 代码

****** 重复点击报错解决:******

方法1:对路径进行判断

methods: {
    handelClick(item) {
      //判断点击路径与点击菜单路径是否不同
      //有效避免重复替换相同路径
      if (item.key !== this.$route.path) {
        this.$router.push(item.key)
        console.log(this.$route.path)
        console.log(item)
      }
    }
  }

方法2:在main.js中添加代码

import VueRouter from 'vue-router'
Vue.use(VueRouter)

const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
    return originalReplace.call(this, location).catch(err => err);
};
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

****** 浏览器刷新/后退 菜单高亮区域:******

完整代码:

<template>
  <a-layout id="components-layout-demo-custom-trigger">
    <a-layout-sider v-model="collapsed" :trigger="null" collapsible>
      <div class="logo" />
      <a-menu theme="dark"
              mode="inline"
              @click="handelClick"
              :selectedKeys="[$route.path]"
      >

        <a-menu-item key='/register'>
          <a-icon type="user" />
          <span>注册</span>
        </a-menu-item>
        <a-menu-item key='/login'>
          <a-icon type="login" />
          <span>登录</span>
        </a-menu-item>
        <a-menu-item key='/modify'>
          <a-icon type="reload" />
          <span>忘记密码</span>
        </a-menu-item>
      </a-menu>
    </a-layout-sider>
    <a-layout>
      <a-layout-header style="background: #fff; padding: 0">
        <a-icon
            class="trigger"
            :type="collapsed ? 'menu-unfold' : 'menu-fold'"
            @click="() => (collapsed = !collapsed)"
        />

        <span>登录注册模块</span>
      </a-layout-header>
      <a-layout-content
          :style="{ margin: '24px 16px', padding: '24px', background: '#fff', minHeight: '280px' }"
      >
        <router-view></router-view>
      </a-layout-content>
    </a-layout>
  </a-layout>
</template>

<script>
export default {
  name: 'Body',
  data() {
    return {
      collapsed: false
    };
  },
  mounted() {
    this.$router.push('/register')
  },
  methods: {
    handelClick(item) {
      if (item.key !== this.$route.path) {
        this.$router.push(item.key)
        //console.log(this.$route.path)
        //console.log(item)
      }
    }
  }
}
</script>

<style>
#components-layout-demo-custom-trigger {
  height: 100%;
}
#components-layout-demo-custom-trigger .trigger {
  font-size: 18px;
  line-height: 64px;
  padding: 0 24px;
  cursor: pointer;
  transition: color 0.3s;
}

#components-layout-demo-custom-trigger .trigger:hover {
  color: #1890ff;
}

#components-layout-demo-custom-trigger .logo {
  height: 32px;
  background: rgba(255, 255, 255, 0.2);
  margin: 16px;
}

</style>

关键代码: 

<a-menu theme="dark"
              mode="inline"
              :default-selected-keys="['1']"
              @click="handelClick"
              :selectedKeys="[$route.path]"
      >

        <a-menu-item key='/register'>
          <a-icon type="user" />
          <span>注册</span>
        </a-menu-item>

/**
*在a-menu中设置的:selectedKeys="key",绑定当前的路由"[$route.path]"
*所以在a-menu-item的key需要做出改变,改为路径
*同时也方便了后续处理点击事件传入的路径
*/

顺便说下菜单的点击事件:

上面好像说了

演示结果:

 

 

最后,有说明不清楚的地方欢迎评论指出👍

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值