欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于antd-vue?a-menu菜單綁定路由的相關(guān)問題

 更新時間:2022年10月10日 10:55:55   作者:晚上八點半  
這篇文章主要介紹了關(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驗證用戶名是否可用的方法

    Vue驗證用戶名是否可用的方法

    這篇文章主要為大家詳細介紹了Vue驗證用戶名是否可用的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • vue實現(xiàn)定時刷新數(shù)據(jù),每隔5分鐘執(zhí)行一次

    vue實現(xiàn)定時刷新數(shù)據(jù),每隔5分鐘執(zhí)行一次

    這篇文章主要介紹了vue實現(xiàn)定時刷新數(shù)據(jù),每隔5分鐘執(zhí)行一次問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • 使用table做成樹形結(jié)構(gòu)的table

    使用table做成樹形結(jié)構(gòu)的table

    這篇文章主要介紹了使用table做成樹形結(jié)構(gòu)的table問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Vuex(多組件數(shù)據(jù)共享的Vue插件)搭建與使用

    Vuex(多組件數(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-10
  • vue監(jiān)聽滾動事件的方法

    vue監(jiān)聽滾動事件的方法

    這篇文章主要介紹了vue監(jiān)聽滾動事件的方法,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下
    2020-12-12
  • 詳解vue-cli腳手架中webpack配置方法

    詳解vue-cli腳手架中webpack配置方法

    這篇文章主要介紹了詳解vue-cli腳手架中webpack配置方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • Vue自定義toast組件的實例代碼

    Vue自定義toast組件的實例代碼

    這篇文章主要介紹了Vue自定義toast組件的相關(guān)資料,需要的朋友可以參考下
    2018-08-08
  • Vue結(jié)合leaflet實現(xiàn)克里金插值

    Vue結(jié)合leaflet實現(xiàn)克里金插值

    本文主要介紹了Vue結(jié)合leaflet實現(xiàn)克里金插值,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-06-06
  • vue-cli的index.html中使用環(huán)境變量方式

    vue-cli的index.html中使用環(huán)境變量方式

    這篇文章主要介紹了vue-cli的index.html中使用環(huán)境變量方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Vue項目部署后首頁白屏問題排查與解決方法

    Vue項目部署后首頁白屏問題排查與解決方法

    在部署 Vue.js 項目時,有時會遇到首頁加載后出現(xiàn)白屏的情況,這可能是由于多種原因造成的,本文將介紹一些常見的排查方法和解決方案,幫助開發(fā)者快速定位問題并解決,感興趣的小伙伴跟著小編一起來看看吧
    2024-08-08

最新評論