基于vue+elementPlus的動(dòng)態(tài)導(dǎo)航標(biāo)簽欄tabs具體過(guò)程
今天來(lái)寫(xiě)一個(gè)導(dǎo)航標(biāo)簽欄,哈哈,我也不清楚這個(gè)功能是不是這個(gè)名字。
今天領(lǐng)導(dǎo)說(shuō)要在系統(tǒng)重添加個(gè)導(dǎo)航標(biāo)簽欄的功能,雖然很簡(jiǎn)單但我是第一次做這個(gè)功能,有些地方可能想的不夠完美,希望如果有更好想法的朋友能指點(diǎn)指點(diǎn)。如果還可以,也希望給各位同行提供一個(gè)小demo,用的時(shí)候可以直接拿來(lái)用。嘻嘻!?。?/p>
實(shí)現(xiàn)思路
首先用到了element-plus框架中tabs的組件,然后數(shù)據(jù)我這里是通過(guò)vuex來(lái)維護(hù)的。在點(diǎn)擊菜單的同時(shí)往vuex相關(guān)數(shù)據(jù)數(shù)組中添加一項(xiàng)數(shù)據(jù)即可,刪除也一樣的道理。具體的實(shí)現(xiàn)如下:
具體過(guò)程
首先,創(chuàng)建一個(gè)HeaderTabs.vue的組件,我們使用時(shí)直接引用即可
<template> <div class="head_tabs"> <keep-alive> <el-tabs v-model="HeaderTabsActiveName" type="card" @tab-click="handlePathSwicth" class="handleTabs" closable @tab-remove="removeHeaderTabsList" > <el-tab-pane v-for="(item,index) in HeaderTabsList" :key="index" :label="item.topic" :name="item.path" > </el-tab-pane> </el-tabs> </keep-alive> </div> </template> <script setup> import { ref ,watch,computed, onMounted} from 'vue' import { useRouter } from 'vue-router' import { useStore } from 'vuex'; // 聲明路由對(duì)象 const router = useRouter(); // 聲明vuex對(duì)象 const store = useStore(); const HeaderTabsActiveName = ref('') const HeaderTabsList = ref([]) onMounted(()=>{ // 這里的數(shù)據(jù)都是在vuex中獲取的 HeaderTabsList.value = store.state.HeaderTabs; HeaderTabsActiveName.value = store.state.TabsActiveName; }) // 這里直接監(jiān)聽(tīng)vuex的數(shù)據(jù)會(huì)導(dǎo)致失效,這里可能是因?yàn)関ue性能問(wèn)題,vue的底層沒(méi)有給vuex的數(shù)據(jù)也進(jìn)行數(shù)據(jù)綁定吧 // 因此,這里使用computed這個(gè)api函數(shù)來(lái)返回vuex中的值,然后將此方法體放入watch中監(jiān)聽(tīng) const getTabsActiveName = computed(()=>{ return store.state.TabsActiveName; }) // 使用watch監(jiān)聽(tīng)vuex的數(shù)據(jù),并開(kāi)啟第一次綁定監(jiān)聽(tīng)--immediate watch(getTabsActiveName,(o1)=>{ if(o1){ // 我們每添加條數(shù)據(jù)都需要修改相關(guān)的tabs索引,否則選中項(xiàng)是不會(huì)高亮的 HeaderTabsActiveName.value = o1; } },{ immediate: true }) // 標(biāo)簽點(diǎn)擊觸發(fā) const handlePathSwicth = (targetName)=>{ let name = targetName.props.name; store.commit("updateTabsActiveName",name); HeaderTabsActiveName.value = name; // 手動(dòng)跳轉(zhuǎn),組件不會(huì)提供跳轉(zhuǎn)功能 router.push({ path:name }) } // 刪除tabs觸發(fā) const removeHeaderTabsList = (targetName) => { // 使用vuex中自定義的方法刪除數(shù)據(jù) store.commit('removeHeaderTabs', targetName) setTimeout(()=>{ // 利用定時(shí)器達(dá)到延時(shí)方法,防止HeaderTabsActiveName還未變就執(zhí)行下面的跳轉(zhuǎn) router.push({ path:HeaderTabsActiveName.value }) },0) } </script>
下面我們開(kāi)始編寫(xiě)vuex中的內(nèi)容
vuex主要的角色是來(lái)維護(hù)我們的數(shù)據(jù),讓我們的數(shù)據(jù)達(dá)到共享的效果,使得我們的數(shù)據(jù)可以在系統(tǒng)中共同使用和維護(hù)。 但是使用vuex時(shí)需要的代碼量要比pinia要麻煩點(diǎn)。因?yàn)楸鞠到y(tǒng)采用的是vuex的數(shù)據(jù)存儲(chǔ)方法,因此我們使用vuex的寫(xiě)法,用pinia編寫(xiě)時(shí)的邏輯都是一樣的。(這里只將關(guān)鍵代碼顯示出來(lái)。vuex的具體用法直接百度即可)
首先在state.ts文件中編寫(xiě)聲明相關(guān)數(shù)據(jù)。
import {ref} from 'vue' //初始化vuex的數(shù)據(jù) const state = { // tabs面包屑的數(shù)據(jù) HeaderTabs:[ { topic:"首頁(yè)", path:'/home' } ], TabsActiveName: ref("/home") } export { state }
然后在mutations.ts文件中編寫(xiě)相關(guān)方法。
// 修改tabs的索引綁定 updateTabsActiveName(state,msg){ // 修改標(biāo)簽索引 state.TabsActiveName = msg }, // 添加tabs標(biāo)簽 addHeaderTabs(state,msg){ // 用filter過(guò)濾器看添加項(xiàng)是否已存在,如果存在則不添加 let f = state.HeaderTabs.filter(t=>{ return t.path == msg.path }) if(f.length==0){ state.HeaderTabs.push(msg) } state.TabsActiveName = msg.path }, // 刪除tabs標(biāo)簽 removeHeaderTabs(state,msg){ // 如果要?jiǎng)h除的是首頁(yè)就直接退出,這里根據(jù)業(yè)務(wù)而定 if(msg=="/home") return; // 通過(guò)循環(huán)方式找到要?jiǎng)h除的數(shù)據(jù)項(xiàng) state.HeaderTabs.forEach((item,index)=>{ if(item.path==msg){ state.HeaderTabs.splice(index,1); return true; } }) // 如果當(dāng)前顯示頁(yè)如刪除項(xiàng)一樣,則自動(dòng)跳轉(zhuǎn)會(huì)首頁(yè),這里也是根據(jù)具體業(yè)務(wù)而定 if(state.TabsActiveName==msg){ state.TabsActiveName = "/home" } }
菜單項(xiàng)調(diào)用添加邏輯
最后在需要添加的tabs標(biāo)簽的地方增加新增按鈕即可,這里直接在系統(tǒng)的菜單欄操作。當(dāng)用戶點(diǎn)擊菜單項(xiàng)時(shí)不僅跳轉(zhuǎn)相關(guān)路由,還調(diào)用剛才相關(guān)的方法,具體代碼如下:
// 這里是LeftMenu.vue中 // 菜單點(diǎn)擊時(shí)觸發(fā)的方法 menuClick(row){ // 將當(dāng)前點(diǎn)擊的對(duì)象傳遞給指定方法 this.store.commit("addHeaderTabs",row) // 跳轉(zhuǎn)相關(guān)路由 this.router.push({ path:row.path }) }
應(yīng)用于界面
最后,最關(guān)鍵的一步,我們現(xiàn)在只是寫(xiě)好了邏輯,但是還沒(méi)在頁(yè)面上顯示呢。接下來(lái)在自己的主界面上引入組件。
不使用setup語(yǔ)法糖時(shí):
<template> <div class="index"> <HeaderTabs></HeaderTabs> </div> </template> <script> import { defineComponent } from '@vue/composition-api' //@ 后是自己組件的真實(shí)路徑 import HeaderTabs from '@/components/page/component/HeaderTabs.vue' export default defineComponent({ components:{ HeaderTabs }, }) </script>
使用setup語(yǔ)法糖:
<template> <div class="index"> <HeaderTabs></HeaderTabs> </div> </template> <script setup> import HeaderTabs from '@/components/page/component/HeaderTabs.vue' </script>
實(shí)現(xiàn)效果
新增:
刪除:
公司系統(tǒng)是保密項(xiàng),不能直接放出來(lái),我直接在自己的畢設(shè)項(xiàng)目上添加的,所有樣式看起來(lái)有點(diǎn)倉(cāng)促。哈哈。
總結(jié)
到此這篇關(guān)于基于vue+elementPlus的動(dòng)態(tài)導(dǎo)航標(biāo)簽欄tabs的文章就介紹到這了,更多相關(guān)vue elementPlus動(dòng)態(tài)導(dǎo)航標(biāo)簽欄tabs內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VUE組件中的 Drawer 抽屜實(shí)現(xiàn)代碼
這篇文章主要介紹了VUE組件 之 Drawer 抽屜 ,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08Vue3中使用styled-components的實(shí)現(xiàn)
本文主要介紹了Vue3中使用styled-components的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05Vuex 在Vue 組件中獲得Vuex 狀態(tài)state的方法
今天小編就為大家分享一篇Vuex 在Vue 組件中獲得Vuex 狀態(tài)state的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08