element-plus默認菜單打開步驟
在 Vue 3 中使用 Element Plus 的 <el-menu> 組件時,默認情況下菜單項是關閉狀態(tài)的。如果你想讓某個菜單項默認處于展開狀態(tài),你可以通過設置菜單項的 default-active 屬性來實現(xiàn)。
默認寫法
步驟 1: 設置 default-active
你需要在 <el-menu> 組件上設置 default-active 屬性,并為其提供一個值,該值應該是你希望默認激活的菜單項的索引或路徑。
示例代碼
假設你有一個簡單的菜單結構,其中包含一個子菜單,你想讓這個子菜單默認展開:
<template>
<el-menu :default-active="activeIndex" class="el-menu-vertical-demo">
<el-sub-menu index="1">
<template #title>
<el-icon><location /></el-icon>
<span>導航一</span>
</template>
<el-menu-item index="1-1">選項1</el-menu-item>
<el-menu-item index="1-2">選項2</el-menu-item>
<el-menu-item index="1-3">選項3</el-menu-item>
</el-sub-menu>
<el-menu-item index="2">
<el-icon><document /></el-icon>
<template #title>導航二</template>
</el-menu-item>
<el-menu-item index="3" disabled>
<el-icon><setting /></el-icon>
<template #title>導航三</template>
</el-menu-item>
</el-menu>
</template>
<script setup>
import { ref } from 'vue';
import { Location, Document, Setting } from '@element-plus/icons-vue';
const activeIndex = ref('1-1'); // 默認激活 "1-1" 菜單項
</script>說明
default-active屬性:設置為'1-1',表示默認激活index="1-1"的菜單項。el-sub-menu:用于創(chuàng)建子菜單。el-menu-item:用于創(chuàng)建菜單項。<el-icon>:用于顯示圖標。<template #title>:用于自定義菜單項的標題。
注意事項
- 如果你想讓一個子菜單默認展開,可以將
default-active設置為該子菜單中的任意一個子菜單項的index。 - 如果你想讓多個子菜單默認展開,可以使用數(shù)組形式的
default-active屬性。
示例:多個子菜單默認展開
如果你想讓多個子菜單默認展開,你可以將 default-active 設置為一個數(shù)組,包含你希望默認激活的菜單項的索引。
<template>
<el-menu :default-active="['1-1', '2']" class="el-menu-vertical-demo">
<el-sub-menu index="1">
<template #title>
<el-icon><location /></el-icon>
<span>導航一</span>
</template>
<el-menu-item index="1-1">選項1</el-menu-item>
<el-menu-item index="1-2">選項2</el-menu-item>
<el-menu-item index="1-3">選項3</el-menu-item>
</el-sub-menu>
<el-menu-item index="2">
<el-icon><document /></el-icon>
<template #title>導航二</template>
</el-menu-item>
<el-menu-item index="3" disabled>
<el-icon><setting /></el-icon>
<template #title>導航三</template>
</el-menu-item>
</el-menu>
</template>
<script setup>
import { ref } from 'vue';
import { Location, Document, Setting } from '@element-plus/icons-vue';
const activeIndex = ref(['1-1', '2']); // 默認激活 "1-1" 和 "2" 菜單項
</script>在這個例子中,default-active 設置為 ['1-1', '2'],表示默認激活 index="1-1" 和 index="2" 的菜單項。這將使得 index="1" 的子菜單及其第一個子菜單項 index="1-1" 處于展開狀態(tài),并且 index="2" 的菜單項也處于激活狀態(tài)。
特殊寫法
Menu 組件
<template>
<template v-for="(item, index) in menuList" :key="index">
<!-- 沒有子路由 -->
<template v-if="!item.children">
<el-menu-item v-if="!item.meta.hidden" :index="item.path" @click="goRoute">
<el-icon>
<component :is="item.meta.icon"></component>
</el-icon>
<template #title>
<span>{{ item.meta.title }}</span>
</template>
</el-menu-item>
</template>
<!-- 只有一個子路由 -->
<template v-if="item.children && item.children.length == 1">
<el-menu-item v-if="!item.children[0].meta.hidden" :index="item.children[0].path" @click="goRoute">
<el-icon>
<component :is="item.children[0].meta.icon"></component>
</el-icon>
<template #title>
<span>{{ item.children[0].meta.title }}</span>
</template>
</el-menu-item>
</template>
<!-- 有多個子路由 -->
<el-sub-menu v-if="item.children && item.children.length > 1" :index="item.path">
<template #title>
<el-icon>
<component :is="item.meta.icon"></component>
</el-icon>
<span>{{ item.meta.title }}</span>
</template>
<Menu :menuList="item.children"></Menu>
</el-sub-menu>
</template>
</template>
<script setup lang="ts">
import { useRouter } from 'vue-router'
// 引入路由器
const $router = useRouter()
// 獲取父組件傳遞的數(shù)據(jù)
defineProps(['menuList'])
// 點擊菜單的回調(diào)
const goRoute = (vc: any) => {
// 路由跳轉
$router.push(vc.index)
}
</script>
<script lang="ts">
export default {
name: 'Menu'
}
</script>
<style scoped></style>菜單欄 組件:
<template>
<div class="layout_container">
<!-- 左側菜單 -->
<div class="layout_slider">
<Logo></Logo>
<!-- 展示菜單 -->
<!-- 滾動組件 -->
<el-scrollbar class="scrollbar">
<!-- 菜單組件 -->
<el-menu background-color="#2e2e2e" text-color="white" active-text-color="yellowgreen" :default-active="$route.path">
<Menu :menuList="userStore.menuRoutes"></Menu>
</el-menu>
</el-scrollbar>
</div>
<!-- 頂部導航 -->
<div class="layout_tabbar">456</div>
<!-- 內(nèi)容展示區(qū)域 -->
<div class="layout_main">
<Main></Main>
</div>
</div>
</template>
<script setup lang="ts">
// 引入左側菜單logo子組件
import Logo from './logo/index.vue'
// 引入菜單組件
import Menu from './menu/index.vue'
// 右側內(nèi)容的展示區(qū)
import Main from './main/index.vue'
// 獲取路由對象
import { useRoute } from 'vue-router';
// 獲取用戶相關的小倉庫
import useUserStore from '@/store/modules/user';
let userStore = useUserStore();
// 獲取路由對象
let $route = useRoute();
</script>
<style scoped lang="scss">
.layout_container {
width: 100%;
height: 100vh;
background-color: red;
.layout_slider {
width: $base-menu-width;
height: 100vh;
background-color: $base-menu-bg;
.scrollbar {
width: $base-menu-width;
height: calc(100vh - $base-menu-logo-height);
.el-menu {
border-right: none;
}
}
}
.layout_tabbar {
position: fixed;
width: calc(100% - $base-menu-width);
height: $base-tabbar-height;
background-color: cyan;
top: 0px;
left: $base-menu-width;
}
.layout_main {
position: fixed;
width: calc(100% - $base-menu-width);
height: calc(100% - $base-tabbar-height);
background-color: yellow;
top: $base-tabbar-height;
left: $base-menu-width;
padding: 20px;
overflow: auto;
}
}
</style>到此這篇關于element-plus默認菜單打開的文章就介紹到這了,更多相關element-plus菜單打開內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- 去除Element-Plus下拉菜單邊框的實現(xiàn)步驟
- 詳解vue3+element-plus實現(xiàn)動態(tài)菜單和動態(tài)路由動態(tài)按鈕(前后端分離)
- vue3+element-plus動態(tài)路由菜單示例代碼
- Vue3 Element-plus el-menu無限級菜單組件封裝過程
- vue3 element-plus二次封裝組件系列之伸縮菜單制作
- Vue3+Element-Plus?實現(xiàn)點擊左側菜單時顯示不同內(nèi)容組件展示在Main區(qū)域功能
- Vue3+Element-Plus實現(xiàn)左側菜單折疊與展開功能示例
- vue3使用element-plus搭建后臺管理系統(tǒng)之菜單管理功能
相關文章
javascript實現(xiàn)動態(tài)表頭及表列的展現(xiàn)方法
這篇文章主要介紹了javascript實現(xiàn)動態(tài)表頭及表列的展現(xiàn)方法,涉及javascript動態(tài)操作table元素的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07
javascript-TreeView父子聯(lián)動效果保持節(jié)點狀態(tài)一致
javascript-TreeView父子聯(lián)動效果保持節(jié)點狀態(tài)一致...2007-08-08
JS判斷元素是否在數(shù)組內(nèi)的實現(xiàn)代碼
這篇文章主要介紹了JS判斷元素是否在數(shù)組內(nèi)的實現(xiàn)代碼,需要的朋友可以參考下2016-03-03
不到200行 JavaScript 代碼實現(xiàn)富文本編輯器的方法
這篇文章主要介紹了不到200行 JavaScript 代碼實現(xiàn)富文本編輯器的方法,需要的朋友可以參考下2018-01-01
javascript function(函數(shù)類型)使用與注意事項小結
這篇文章主要介紹了javascript function(函數(shù)類型)使用與注意事項,結合實例形式較為詳細的分析了Function(函數(shù))類型的基本聲明、屬性、方法相關操作技巧與使用注意事項,需要的朋友可以參考下2019-06-06

