關(guān)于antd-vue?a-menu菜單綁定路由的相關(guān)問(wèn)題
tips: 路由綁定、菜單跳轉(zhuǎn)、網(wǎng)頁(yè)后退高亮顯示
1. 問(wèn)題描述
使用antd-vue 的 a-layout布局和a-menu菜單做一個(gè)側(cè)邊欄菜單,加入vuex配置側(cè)邊欄點(diǎn)擊事件,實(shí)現(xiàn)點(diǎn)擊菜單改變路由展示中間部分內(nèi)容的功能
但是出現(xiàn)了問(wèn)題:
- 重復(fù)點(diǎn)擊路由報(bào)錯(cuò)
- 瀏覽器刷新/后退 菜單高亮區(qū)域沒(méi)有根據(jù)路由的變化產(chǎn)生變化
2. 解決方法
- 對(duì)路由變化進(jìn)行判斷/修改router 的push與replace方法
- 借助a-menu 的屬性::selectedKeys綁定路由地址,就能實(shí)現(xiàn)隨著路由產(chǎn)生變化
3. 代碼
****** 重復(fù)點(diǎn)擊報(bào)錯(cuò)解決:******
方法1:對(duì)路徑進(jìn)行判斷
methods: { ? ? handelClick(item) { ? ? ? //判斷點(diǎn)擊路徑與點(diǎn)擊菜單路徑是否不同 ? ? ? //有效避免重復(fù)替換相同路徑 ? ? ? 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>注冊(cè)</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>登錄注冊(cè)模塊</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>注冊(cè)</span> ? ? ? ? </a-menu-item> ? /** *在a-menu中設(shè)置的:selectedKeys="key",綁定當(dāng)前的路由"[$route.path]" *所以在a-menu-item的key需要做出改變,改為路徑 *同時(shí)也方便了后續(xù)處理點(diǎn)擊事件傳入的路徑 */
順便說(shuō)下菜單的點(diǎn)擊事件:
上面好像說(shuō)了
演示結(jié)果:
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue實(shí)現(xiàn)定時(shí)刷新數(shù)據(jù),每隔5分鐘執(zhí)行一次
這篇文章主要介紹了vue實(shí)現(xiàn)定時(shí)刷新數(shù)據(jù),每隔5分鐘執(zhí)行一次問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01使用table做成樹(shù)形結(jié)構(gòu)的table
這篇文章主要介紹了使用table做成樹(shù)形結(jié)構(gòu)的table問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07Vuex(多組件數(shù)據(jù)共享的Vue插件)搭建與使用
Vuex是實(shí)現(xiàn)組件全局狀態(tài)(數(shù)據(jù))管理的一種機(jī)制,可以方便的實(shí)現(xiàn)組件之間數(shù)據(jù)的共享,數(shù)據(jù)緩存等等,下面這篇文章主要給大家介紹了關(guān)于Vuex(多組件數(shù)據(jù)共享的Vue插件)搭建與使用的相關(guān)資料,需要的朋友可以參考下2022-10-10vue監(jiān)聽(tīng)滾動(dòng)事件的方法
這篇文章主要介紹了vue監(jiān)聽(tīng)滾動(dòng)事件的方法,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下2020-12-12Vue結(jié)合leaflet實(shí)現(xiàn)克里金插值
本文主要介紹了Vue結(jié)合leaflet實(shí)現(xiàn)克里金插值,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06vue-cli的index.html中使用環(huán)境變量方式
這篇文章主要介紹了vue-cli的index.html中使用環(huán)境變量方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10Vue項(xiàng)目部署后首頁(yè)白屏問(wèn)題排查與解決方法
在部署 Vue.js 項(xiàng)目時(shí),有時(shí)會(huì)遇到首頁(yè)加載后出現(xiàn)白屏的情況,這可能是由于多種原因造成的,本文將介紹一些常見(jiàn)的排查方法和解決方案,幫助開(kāi)發(fā)者快速定位問(wèn)題并解決,感興趣的小伙伴跟著小編一起來(lái)看看吧2024-08-08