關(guān)于antd-vue?a-menu菜單綁定路由的相關(guān)問題
tips: 路由綁定、菜單跳轉(zhuǎn)、網(wǎng)頁后退高亮顯示
1. 問題描述
使用antd-vue 的 a-layout布局和a-menu菜單做一個側(cè)邊欄菜單,加入vuex配置側(cè)邊欄點擊事件,實現(xiàn)點擊菜單改變路由展示中間部分內(nèi)容的功能
但是出現(xiàn)了問題:
- 重復點擊路由報錯
- 瀏覽器刷新/后退 菜單高亮區(qū)域沒有根據(jù)路由的變化產(chǎn)生變化
2. 解決方法
- 對路由變化進行判斷/修改router 的push與replace方法
- 借助a-menu 的屬性::selectedKeys綁定路由地址,就能實現(xiàn)隨著路由產(chǎn)生變化
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) } ****** 瀏覽器刷新/后退 菜單高亮區(qū)域:******
完整代碼:
<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>
關(guān)鍵代碼:
<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需要做出改變,改為路徑 *同時也方便了后續(xù)處理點擊事件傳入的路徑 */
順便說下菜單的點擊事件:
上面好像說了
演示結(jié)果:
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue實現(xiàn)定時刷新數(shù)據(jù),每隔5分鐘執(zhí)行一次
這篇文章主要介紹了vue實現(xiàn)定時刷新數(shù)據(jù),每隔5分鐘執(zhí)行一次問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01Vuex(多組件數(shù)據(jù)共享的Vue插件)搭建與使用
Vuex是實現(xiàn)組件全局狀態(tài)(數(shù)據(jù))管理的一種機制,可以方便的實現(xiàn)組件之間數(shù)據(jù)的共享,數(shù)據(jù)緩存等等,下面這篇文章主要給大家介紹了關(guān)于Vuex(多組件數(shù)據(jù)共享的Vue插件)搭建與使用的相關(guān)資料,需要的朋友可以參考下2022-10-10Vue結(jié)合leaflet實現(xiàn)克里金插值
本文主要介紹了Vue結(jié)合leaflet實現(xiàn)克里金插值,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-06-06vue-cli的index.html中使用環(huán)境變量方式
這篇文章主要介紹了vue-cli的index.html中使用環(huán)境變量方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10