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

vue3+element-plus 實(shí)現(xiàn)動(dòng)態(tài)菜單和動(dòng)態(tài)路由的渲染的項(xiàng)目實(shí)踐

 更新時(shí)間:2024年11月08日 11:19:44   作者:gt8011  
本文主要介紹了vue3+element-plus 實(shí)現(xiàn)動(dòng)態(tài)菜單和動(dòng)態(tài)路由的渲染的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

在 Vue.js 中,使用 Vue Router 管理路由數(shù)據(jù),并將其用于渲染 el-menu(Element UI 的菜單組件)通常涉及以下幾個(gè)步驟:

  • 定義路由元數(shù)據(jù)
    在你的路由配置中,為每個(gè)路由項(xiàng)添加 meta 字段,這個(gè)字段可以包含任何你想要傳遞給菜單的數(shù)據(jù),例如菜單名稱、圖標(biāo)等。

  • 獲取路由數(shù)據(jù)
    使用 router 實(shí)例的 getRoutes() 方法來獲取當(dāng)前注冊(cè)的所有路由信息。

  • 過濾和格式化數(shù)據(jù)
    根據(jù)需要過濾和格式化路由數(shù)據(jù),以便它們可以被 el-menu 組件使用。

  • 將數(shù)據(jù)傳遞給組件
    將格式化好的路由數(shù)據(jù)傳遞給使用 el-menu 的組件。

  • 使用 v-for 渲染菜單
    在組件的模板中,使用 v-for 指令來遍歷路由數(shù)據(jù),并渲染 el-menu。

實(shí)現(xiàn)效果

在這里插入圖片描述

路由配置

首先是路由配置,要實(shí)現(xiàn)這個(gè)路由配置,你需要完成以下幾個(gè)步驟:

  • 注冊(cè) Vue Router:確保你已經(jīng)在你的 Vue 應(yīng)用中安裝并注冊(cè)了 Vue Router。

  • 定義路由:使用 Vue Router 的 addRoute 方法動(dòng)態(tài)添加路由。

  • 使用路由:在應(yīng)用中使用 <router-view> 來渲染對(duì)應(yīng)的組件。

  • 渲染菜單:如果你需要根據(jù)路由配置動(dòng)態(tài)渲染菜單(例如使用 Element UI 的 el-menu),你需要從路由配置中提取必要的信息。

實(shí)現(xiàn)示例:

main.js

import { createApp } from 'vue';
import App from './App.vue';
import router from './router'; // 假設(shè)你有一個(gè) router 文件

const app = createApp(App);

app.use(router).mount('#app');

router.js

import { createRouter, createWebHistory } from 'vue-router';
import Main from '../views/Main.vue';
import Home from '../views/Home.vue';
import Goods from '../views/goods.vue';
import Order from '../views/order.vue';

const routes = [
  {
    path: '/',
    name: 'main',
    component: Main,
    children: [
      {
        path: 'home',
        name: 'home',
        component: Home,
        meta: { title: '主頁', icon: 'house' },
      },
      {
        path: 'goods',
        name: 'Goods',
        component: Goods,
        meta: { title: '商品列表', icon: 'el-icon-shopping-cart-full' },
      },
      {
        path: 'order',
        name: 'Order',
        redirect: 'order/table',
        children: [
          {
            path: 'table',
            name: 'Table',
            component: Order,
            meta: { title: '訂單列表', icon: 'el-icon-s-fold' },
          },
        ],
        meta: { title: '訂單', icon: 'el-icon-s-claim' },
      },
    ],
  },
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

export default router;

App.vue

<template>
  <div id="app">
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name: 'App',
};
</script>

動(dòng)態(tài)渲染菜單

如果你想根據(jù)路由配置動(dòng)態(tài)渲染菜單(el-menu),你可以在組件中使用 Vue Router 的 routes 屬性。以下是一個(gè)使用組合式 API 的示例:

MenuComponent.vue

<template>
    <el-aside :width="width">
        <el-menu
        background-color="#fff"
        active-text-color="#409eff"
        class="aside-menu"
        :collapse="isCollapse"
        :default-active="activeMenu"
        >
        <el-menu-item 
        v-for="item in noChilden"
        :index="item.path"
        :key="item.path"
        @click="handlemenu(item)"
        >
          <component class="icon" :is="item.meta.icon"></component>
           <span>{{ item.meta.title }}</span>        
        </el-menu-item>
        <el-sub-menu 
        v-for="item in hasChilden"
        :index="item.path"
        :key="item.path"
        >
          <template #title>
            <component class="icon" :is="item.meta.icon"></component>
            <span>{{ item.meta.title }}</span>
          </template>
          <!-- <el-menu-item-group> -->
            <el-menu-item 
            v-for="subItem in item.children"
           :key="subItem.path"
           :index="subItem.path"
           @click="handlemenuchild(item,subItem)"
           class="sub-menu-deep"
           
            >
              <component class="icon" :is="subItem.meta.icon"></component>
              <span>{{ subItem.meta.title }}</span>
            </el-menu-item>
          <!-- </el-menu-item-group> -->
        </el-sub-menu>
       
      </el-menu>
    </el-aside>
</template>

<script setup>

import {ref,computed,onMounted} from 'vue'
import {useAllDataStore} from "@/store"
import {useRouter,useRoute} from 'vue-router'

const list = ref([]);
const router = useRouter();
onMounted(() =>{
//找到根路由
const childRoutes = router.getRoutes().find(route => route.path === '/').children; 

list.value = childRoutes.filter(route => route.meta && route.meta.title);
console.log(list.value);
});
const hasChilden = computed(() => list.value.filter(item => item.children && item.children.length > 0))
const noChilden = computed(() => list.value.filter(item => !item.children || item.children.length === 0))

const route = useRoute()
const handlemenu = (item) =>{
  router.push(item.path)
 
}
const handlemenuchild = (item,subItem) =>{
  router.push(item.path+'/'+ subItem.path)
}
const activeMenu = computed(() => route.path)
</script>

<style lang="less" scoped>
.icon{
  width:18px;
  height: 18px;
  margin-right: 5px;
}
.el-menu {
  border-right: none;
  font-size: 20px;
  font-weight: bold;
  h3{
    line-height: 48px;
    text-align: center;
  }
}
.el-aside{
    height:100%;
    background-color: #fff;
    // box-shadow: 1px 0px 5px rgba(0, 0, 0, 0.2);
    border-right: 1px solid #e4e7ed;
}

</style>解釋上面是怎么實(shí)現(xiàn)router 動(dòng)態(tài)渲染菜單的

這段代碼是一個(gè)使用 Vue 3 和 Element UI 庫實(shí)現(xiàn)的側(cè)邊導(dǎo)航菜單組件,它通過 Vue Router 的路由信息動(dòng)態(tài)渲染菜單項(xiàng)。下面是代碼的詳細(xì)解釋:

布局中引入組件

<script setup>
import CommonAside from '@/components/.vue'
import CommonHeader from '../components/MenuComponent.vue';
import CommonTab from '@/components/CommonTab.vue'
</script>

<template>
   <div class="common-layput">
    <el-container class="lay-container">
         <!-- 自定義左側(cè)菜單組件 -->
         <menu-component />
    <el-container>
    <el-header class="el-header">
        <common-header />
    </el-header>
    <el-main class="right-mian">
          <!-- 路由顯示內(nèi)容 -->
      <router-view></router-view>
    </el-main>
    </el-container>
    </el-container>
   </div>
</template>

<style scoped lang="less">
   .common-layput,.lay-container{
      height:100%;
   }
   .el-header{
      background-color: #fff;
      box-shadow: 1px 0px 5px rgba(0, 0, 0, 0.2);
   }
</style>

模板部分 (<template>)

  • 使用 el-aside 組件作為側(cè)邊欄容器。
  • el-menu 組件用于創(chuàng)建菜單,其中:
    • background-color 和 active-text-color 設(shè)置菜單的背景色和激活項(xiàng)的文字顏色。
    • class 添加自定義類名。
    • :collapse 屬性用于控制菜單的展開和收起狀態(tài)。
    • :default-active 綁定當(dāng)前激活的菜單項(xiàng)路徑。

菜單項(xiàng)和子菜單項(xiàng)渲染

  • 使用 el-menu-item 組件渲染沒有子菜單的頂級(jí)菜單項(xiàng)。
  • 使用 el-sub-menu 組件渲染帶有子菜單的菜單項(xiàng)。

腳本部分 (<script setup>)

  • 引入必要的 Vue 組合式 API 鉤子:refcomputedonMounteduseRouteruseRoute。
  • list 引用用于存儲(chǔ)從 Vue Router 獲取的路由信息。

路由信息獲取

  • 在 onMounted 鉤子中,使用 useRouter 鉤子的 getRoutes 方法找到根路由 '/' 的子路由,并篩選出包含 meta 和 meta.title 的路由,存儲(chǔ)到 list.value。

計(jì)算屬性

  • hasChilden:計(jì)算屬性,篩選出 list.value 中具有子菜單的路由。
  • noChilden:計(jì)算屬性,篩選出 list.value 中沒有子菜單的路由。

導(dǎo)航處理函數(shù)

  • handlemenu:處理無子菜單項(xiàng)的點(diǎn)擊事件,使用 router.push 方法導(dǎo)航到點(diǎn)擊的菜單項(xiàng)路徑。
  • handlemenuchild:處理有子菜單項(xiàng)的點(diǎn)擊事件,拼接父菜單和子菜單的路徑,然后導(dǎo)航。

激活狀態(tài)

  • activeMenu:計(jì)算屬性,根據(jù)當(dāng)前路由的路徑 route.path 設(shè)置激活的菜單項(xiàng)。

樣式部分 (<style>)

  • 定義了一些基本的樣式來美化菜單,例如移除邊框、設(shè)置字體大小和加粗。

總結(jié)

這個(gè)組件通過 Vue Router 的 useRouter 鉤子獲取當(dāng)前路由的配置信息,并根據(jù)這些信息動(dòng)態(tài)生成菜單項(xiàng)。使用 computed 屬性來區(qū)分哪些路由有子菜單,哪些沒有,然后相應(yīng)地渲染 el-menu-item 或 el-sub-menu。點(diǎn)擊菜單項(xiàng)時(shí),使用 router.push 方法來改變頁面的路由,實(shí)現(xiàn)導(dǎo)航功能。并通過 useRoute 鉤子獲取當(dāng)前激活的路由,然后設(shè)置 activeMenu 來決定哪個(gè)菜單項(xiàng)應(yīng)該處于激活狀態(tài)。這個(gè)組件是一個(gè)結(jié)合 Vue Router 和 Element UI 的動(dòng)態(tài)菜單實(shí)現(xiàn),它可以自動(dòng)根據(jù)路由配置渲染出相應(yīng)的菜單結(jié)構(gòu),并且能夠響應(yīng)路由變化,高亮顯示當(dāng)前激活的菜單項(xiàng)。

到此這篇關(guān)于vue3+element-plus 實(shí)現(xiàn)動(dòng)態(tài)菜單和動(dòng)態(tài)路由的渲染的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)vue3+element-plus 動(dòng)態(tài)菜單和動(dòng)態(tài)路由內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue通過?API?監(jiān)聽數(shù)組的變化

    vue通過?API?監(jiān)聽數(shù)組的變化

    這篇文章主要介紹了vue通過?API?監(jiān)聽數(shù)組的變化,在?Vue?中,你可以通過監(jiān)聽數(shù)組的變化來更新界面,Vue?提供了一些特殊的語法以及?API?來實(shí)現(xiàn)對(duì)數(shù)組的監(jiān)聽,本文通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • Vue 如何import服務(wù)器上的js配置文件

    Vue 如何import服務(wù)器上的js配置文件

    這篇文章主要介紹了Vue 如何import服務(wù)器上的js配置文件,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下
    2021-04-04
  • Vue 實(shí)現(xiàn)可視化拖拽頁面編輯器

    Vue 實(shí)現(xiàn)可視化拖拽頁面編輯器

    這篇文章主要介紹了Vue 實(shí)現(xiàn)可視化拖拽頁面編輯器的方法,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下
    2021-02-02
  • vue實(shí)現(xiàn)ajax滾動(dòng)下拉加載,同時(shí)具有l(wèi)oading效果(推薦)

    vue實(shí)現(xiàn)ajax滾動(dòng)下拉加載,同時(shí)具有l(wèi)oading效果(推薦)

    這篇文章主要介紹了vue實(shí)現(xiàn)ajax滾動(dòng)下拉加載,同時(shí)具有l(wèi)oading效果的實(shí)現(xiàn)代碼,文章包括難點(diǎn)說明,介紹的非常詳細(xì),感興趣的朋友參考下
    2017-01-01
  • vue在組件中使用v-model的場景

    vue在組件中使用v-model的場景

    這篇文章主要介紹了vue在組件中使用v-model的場景,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-07-07
  • Vue.js中兄弟組件之間互相傳值實(shí)例

    Vue.js中兄弟組件之間互相傳值實(shí)例

    本篇文章主要介紹了Vue.js中兄弟組件之間互相傳值實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • vue項(xiàng)目引入遠(yuǎn)程jweixin-1.2.0.js文件并使用詳解

    vue項(xiàng)目引入遠(yuǎn)程jweixin-1.2.0.js文件并使用詳解

    這篇文章主要介紹了vue項(xiàng)目引入遠(yuǎn)程jweixin-1.2.0.js文件并使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • vue watch監(jiān)聽變量值的實(shí)時(shí)變動(dòng)示例詳解

    vue watch監(jiān)聽變量值的實(shí)時(shí)變動(dòng)示例詳解

    這篇文章主要介紹了vue 監(jiān)聽變量值的實(shí)時(shí)變動(dòng) watch,使用字符串形式的偵聽器函數(shù),還有一種是使用函數(shù)形式的偵聽器函數(shù),本文通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-08-08
  • vue3使用Pinia修改state的三種方法(直接修改,$patch,actions)

    vue3使用Pinia修改state的三種方法(直接修改,$patch,actions)

    Vue3?Pinia是一個(gè)狀態(tài)管理庫,專門為Vue3設(shè)計(jì)優(yōu)化,它提供了一種簡單而強(qiáng)大的方式來管理應(yīng)用程序的狀態(tài),并且與Vue3的響應(yīng)式系統(tǒng)緊密集成,本文給大家介紹了vue3使用Pinia修改state的三種方法,需要的朋友可以參考下
    2024-03-03
  • vue router 用戶登陸功能的實(shí)例代碼

    vue router 用戶登陸功能的實(shí)例代碼

    這篇文章主要介紹了vue router 用戶登陸功能的實(shí)例代碼,主要用到H5中的會(huì)話存儲(chǔ)(sessionStorage)、vue-router路由前置操作、路由元信息(meta)等知識(shí)點(diǎn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2019-04-04

最新評(píng)論