vue+elementUI如何實(shí)現(xiàn)頂部路由標(biāo)簽跳轉(zhuǎn)
引言
后臺(tái)管理系統(tǒng)中,為了方便使用者跳轉(zhuǎn)已經(jīng)訪問過的頁面,需要實(shí)現(xiàn)頂部路由標(biāo)簽跳轉(zhuǎn)到訪問過頁面的功能,因此封裝成組件方便使用
封裝組件
<!-- TagsView/levelScroll.vue --> <template> <el-scrollbar ref="scrollContainer" :vertical="false" class="scroll-container" @wheel.native.prevent="handleScroll" > <slot /> </el-scrollbar> </template>
<script lang="ts"> import Vue from 'vue'; export default Vue.extend({ data () { return { left: 0 }; }, computed: { scrollWrapper () { return (this.$refs as any).scrollContainer.$refs.wrap; } }, methods: { // 鼠標(biāo)滑動(dòng)事件 handleScroll (e: { wheelDelta: number; deltaY: number }) { const eventDelta = e.wheelDelta || -e.deltaY * 40; const $scrollWrapper = this.scrollWrapper; $scrollWrapper.scrollLeft = $scrollWrapper.scrollLeft + eventDelta / 4; } } }); </script>
<style lang="less" scoped> .scroll-container { white-space: nowrap; position: relative; overflow: hidden; width: 100%; /deep/.el-scrollbar__bar { bottom: 0px; } /deep/.el-scrollbar__wrap { height: 49px; // margin-bottom: -17px !important; // margin-right: -17px !important; } } </style>
vuex 文件
// modules/tagsView.ts const state = { // 訪問過的頁面標(biāo)簽 visitedViews: [ { path: '/dashboard/index', meta: { title: '首頁', affix: true } } ], // 訪問過的頁面的 name 值 cachedViews: [] }; const mutations = { // 添加訪問過的路由 ADD_VISITED_VIEW: (state: { visitedViews: any[] }, view: any) => { if (state.visitedViews.some((v) => v.path === view.path)) return; state.visitedViews.push( Object.assign({}, view, { title: view.meta.title }) ); }, // 添加訪問過的路由 name ADD_CACHED_VIEW: (state: { cachedViews: any[] }, view: { name: string }) => { if (state.cachedViews.includes(view.name)) return; state.cachedViews.push(view.name); }, // 刪除當(dāng)前路由 DEL_VISITED_VIEW: ( state: { visitedViews: any[] }, view: { path: string } ) => { for (const [i, v] of state.visitedViews.entries()) { if (v.path === view.path) { state.visitedViews.splice(i, 1); break; } } }, // 刪除當(dāng)前路由 name DEL_CACHED_VIEW: (state: { cachedViews: any[] }, view: { name: string }) => { const index = state.cachedViews.indexOf(view.name); index > -1 && state.cachedViews.splice(index, 1); }, // 刪除當(dāng)前路由之外的路由 DEL_OTHERS_VISITED_VIEWS: ( state: { visitedViews: any[] }, view: { path: string } ) => { state.visitedViews = state.visitedViews.filter((v) => { return v.meta.affix || v.path === view.path; }); }, // 刪除當(dāng)前路由之外的路由 name DEL_OTHERS_CACHED_VIEWS: ( state: { cachedViews: any[] }, view: { name: string } ) => { const index = state.cachedViews.indexOf(view.name); if (index > -1) { state.cachedViews = state.cachedViews.slice(index, index + 1); } else { state.cachedViews = []; } }, // 刪除全部訪問過的路由 DEL_ALL_VISITED_VIEWS: (state: { visitedViews: any[] }) => { const affixTags = state.visitedViews.filter((tag) => tag.meta.affix); state.visitedViews = affixTags; }, // 刪除全部訪問過的路由 name DEL_ALL_CACHED_VIEWS: (state: { cachedViews: any[] }) => { state.cachedViews = []; } }; const actions = { // 添加訪問過的路由 addVisitedView ({ commit }: { commit: any }, view: any) { commit('ADD_VISITED_VIEW', view); }, // 添加訪問過的路由 name addCachedView ({ commit }: { commit: any }, view: any) { commit('ADD_CACHED_VIEW', view); }, addView ({ dispatch }: { dispatch: any }, view: any) { dispatch('addVisitedView', view); dispatch('addCachedView', view); }, // 刪除當(dāng)前路由 delVisitedView ({ commit }: { commit: any }, view: any) { commit('DEL_VISITED_VIEW', view); }, // 刪除當(dāng)前路由 name delCachedView ({ commit }: { commit: any }, view: any) { commit('DEL_CACHED_VIEW', view); }, delView ({ dispatch }: { dispatch: any }, view: any) { dispatch('delVisitedView', view); dispatch('delCachedView', view); }, // 刪除當(dāng)前路由之外的路由 delOthersVisitedViews ({ commit }: { commit: any }, view: any) { commit('DEL_OTHERS_VISITED_VIEWS', view); }, // 刪除當(dāng)前路由之外的路由 namw delOthersCachedViews ({ commit }: { commit: any }, view: any) { commit('DEL_OTHERS_CACHED_VIEWS', view); }, delOthersViews ({ dispatch }: { dispatch: any }, view: any) { dispatch('delOthersVisitedViews', view); dispatch('delOthersCachedViews', view); }, // 刪除全部訪問過的路由 delAllVisitedViews ({ commit }: { commit: any }) { commit('DEL_ALL_VISITED_VIEWS'); }, // 刪除全部訪問過的路由 name delAllCachedViews ({ commit }: { commit: any }) { commit('DEL_ALL_CACHED_VIEWS'); }, delAllViews ({ dispatch }: { dispatch: any }, view: any) { dispatch('delAllVisitedViews', view); dispatch('delAllCachedViews', view); } }; export default { namespaced: true, state, mutations, actions };
// getters.ts const getters = { // 訪問的頁面標(biāo)簽 visitedViews: (state: any) => state.tagsView.visitedViews, // 訪問的頁面標(biāo)簽 name cachedViews: (state: any) => state.tagsView.cachedViews }; export default getters;
router 文件
import Vue from 'vue'; import VueRouter from 'vue-router'; import Login from '@/views/login/index.vue'; import Layout from '@/layout/index.vue'; Vue.use(VueRouter); /** * hidden 表示是否需要在側(cè)邊導(dǎo)航欄出現(xiàn) ,true表示不需要 * isFirst 表示是否只有一級(jí)權(quán)限,只出現(xiàn)在只有一個(gè)子集,沒有其他孫子集 * 當(dāng)權(quán)限擁有多個(gè)子集或者孫子集,一級(jí)權(quán)限需要加上 meta * 二級(jí)權(quán)限擁有子集,也必須有 meta */ // 基礎(chǔ)路由 export const constantRoutes = [ { path: '/redirect', component: Layout, hidden: true, children: [ { path: '/redirect/:path(.*)', component: () => import('@/views/redirect/index.vue') } ] }, { path: '/', redirect: '/dashboard', hidden: true }, { path: '/login', name: 'Login', component: Login, hidden: true }, { path: '/dashboard', component: Layout, redirect: '/dashboard/index', isFirst: true, children: [ { path: 'index', name: 'Dashboard', component: () => import('@/views/dashboard/index.vue'), meta: { title: '首頁', icon: 'el-icon-location' } } ] } ]; // 動(dòng)態(tài)路由 export const asyncRoutes = [ { path: '/form', component: Layout, redirect: '/form/index', isFirst: true, children: [ { path: 'index', name: 'Form', component: () => import('@/views/form/index.vue'), meta: { title: '表單', role: 'form', icon: 'el-icon-location' } } ] }, { path: '/editor', component: Layout, redirect: '/editor/index', meta: { role: 'editors', title: '總富文本', icon: 'el-icon-location' }, children: [ { path: 'index', name: 'Editor', component: () => import('@/views/editor/index.vue'), meta: { title: '富文本', role: 'editor', icon: 'el-icon-location' } }, { path: 'two', name: 'Two', redirect: '/editor/two/three', component: () => import('@/views/editor/two.vue'), meta: { title: '二級(jí)導(dǎo)航', role: 'two', icon: 'el-icon-location' }, children: [ { path: 'three', name: 'Three', component: () => import('@/views/editor/three.vue'), meta: { title: '三級(jí)導(dǎo)航', role: 'three', icon: 'el-icon-location' } }, { path: 'four', name: 'Four', component: () => import('@/views/editor/four.vue'), meta: { title: '三級(jí)導(dǎo)航2', role: 'four', icon: 'el-icon-location' } } ] } ] }, { path: '/tree', component: Layout, redirect: '/tree/index', isFirst: true, children: [ { path: 'index', name: 'Tree', component: () => import('@/views/tree/index.vue'), meta: { title: '樹狀圖', role: 'tree', icon: 'el-icon-location' } } ] }, { path: '/excel', component: Layout, redirect: '/excel/index', isFirst: true, children: [ { path: 'index', name: 'Excel', component: () => import('@/views/excel/index.vue'), meta: { title: '導(dǎo)入導(dǎo)出', role: 'excel', icon: 'el-icon-location' } } ] } ]; // 出錯(cuò)跳轉(zhuǎn)的路由 export const error = [ // 404 { path: '/404', component: () => import('@/views/error/index.vue'), hidden: true }, { path: '*', redirect: '/404', hidden: true } ]; const createRouter = () => new VueRouter({ scrollBehavior: () => ({ x: 0, y: 0 }), routes: constantRoutes }); const router = createRouter(); // 刷新路由 export function resetRouter () { const newRouter = createRouter(); (router as any).matcher = (newRouter as any).matcher; } export default router;
頁面中使用
<!-- 一般在主體布局頁面使用 --> <template> <div class="layout"> ...... <el-container :class="{ hideSidebar: isCollapse }"> ...... <!-- 主體內(nèi)容 --> <el-main> <!-- 訪問過的路由標(biāo)簽 --> <tags-view></tags-view> <keep-alive :include="cachedViews"> <router-view :key="$route.path" /> </keep-alive> </el-main> </el-container> </div> </template> <script lang="ts"> import Vue from 'vue'; import { mapGetters } from 'vuex'; import SubMenu from '@/components/SubMenu/index.vue'; import MyBreadcrumb from '@/components/Breadcrumb/index.vue'; import TagsView from '@/components/TagsView/index.vue'; import { resetRouter } from '@/router'; export default Vue.extend({ computed: { // 路由 ...mapGetters(['cachedViews']) }, components: { TagsView } }); </script>
總結(jié)
參考網(wǎng)上資料進(jìn)行封裝修改,具體需求可根據(jù)項(xiàng)目修改
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue.js實(shí)現(xiàn)動(dòng)態(tài)面包屑
這篇文章主要為大家詳細(xì)介紹了vue.js實(shí)現(xiàn)動(dòng)態(tài)面包屑,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04mockjs+vue頁面直接展示數(shù)據(jù)的方法
這篇文章主要介紹了mockjs+vue頁面直接展示數(shù)據(jù)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12vue使用webPack打包發(fā)布后頁面顯示空白的解決
這篇文章主要介紹了vue使用webPack打包發(fā)布后頁面顯示空白的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06Element中table組件(el-table)右側(cè)滾動(dòng)條空白占位處理
當(dāng)我設(shè)置了max-height,就會(huì)在表格右側(cè)出現(xiàn)一列空白的占位,本文主要介紹了Element中table組件(el-table)右側(cè)滾動(dòng)條空白占位處理,感興趣的可以了解一下2023-09-09vue3報(bào)錯(cuò)提示找不到模塊“./XXX.vue”或其相應(yīng)的類型聲明問題
這篇文章主要介紹了vue3報(bào)錯(cuò)提示找不到模塊“./XXX.vue”或其相應(yīng)的類型聲明問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06