SoFunction
Updated on 2025-04-11

How to implement top routing tag jump by vue+elementUI

introduction

In the background management system, in order to facilitate users to jump to the pages that have been visited, the function of jumping to the pages that have been visited is to be implemented, so it is encapsulated into components and is convenient for use.

Package components

<!-- TagsView/ -->    
<template>
  <el-scrollbar
    ref="scrollContainer"
    :vertical="false"
    class="scroll-container"
    @="handleScroll"
  >
    <slot />
  </el-scrollbar>
</template>
&lt;script lang="ts"&gt;
import Vue from 'vue';

export default ({
  data () {
    return {
      left: 0
    };
  },
  computed: {
    scrollWrapper () {
      return (this.$refs as any).scrollContainer.$;
    }
  },
  methods: {
    // Mouse swipe event    handleScroll (e: { wheelDelta: number; deltaY: number }) {
      const eventDelta =  || - * 40;
      const $scrollWrapper = ;
      $ = $ + eventDelta / 4;
    }
  }
});
&lt;/script&gt;
<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 file

// modules/
const state = {
  // Visited page tags  visitedViews: [
    {
      path: '/dashboard/index',
      meta: { title: 'front page', affix: true }
    }
  ],
  // The name value of the visited page  cachedViews: []
};

const mutations = {
  // Add the visited route  ADD_VISITED_VIEW: (state: { visitedViews: any[] }, view: any) =&gt; {
    if (((v) =&gt;  === )) return;
    (
      ({}, view, {
        title: 
      })
    );
  },
  // Add the visited route name  ADD_CACHED_VIEW: (state: { cachedViews: any[] }, view: { name: string }) =&gt; {
    if (()) return;
    ();
  },

  // Delete the current route  DEL_VISITED_VIEW: (
    state: { visitedViews: any[] },
    view: { path: string }
  ) =&gt; {
    for (const [i, v] of ()) {
      if ( === ) {
        (i, 1);
        break;
      }
    }
  },
  // Delete the current route name  DEL_CACHED_VIEW: (state: { cachedViews: any[] }, view: { name: string }) =&gt; {
    const index = ();
    index &gt; -1 &amp;&amp; (index, 1);
  },

  // Delete routes outside the current route  DEL_OTHERS_VISITED_VIEWS: (
    state: { visitedViews: any[] },
    view: { path: string }
  ) =&gt; {
     = ((v) =&gt; {
      return  ||  === ;
    });
  },
  // Delete routes outside the current route name  DEL_OTHERS_CACHED_VIEWS: (
    state: { cachedViews: any[] },
    view: { name: string }
  ) =&gt; {
    const index = ();
    if (index &gt; -1) {
       = (index, index + 1);
    } else {
       = [];
    }
  },

  // Delete all accessed routes  DEL_ALL_VISITED_VIEWS: (state: { visitedViews: any[] }) =&gt; {
    const affixTags = ((tag) =&gt; );
     = affixTags;
  },
  // Delete all accessed routes name  DEL_ALL_CACHED_VIEWS: (state: { cachedViews: any[] }) =&gt; {
     = [];
  }
};

const actions = {
  // Add the visited route  addVisitedView ({ commit }: { commit: any }, view: any) {
    commit('ADD_VISITED_VIEW', view);
  },
  // Add the visited route name  addCachedView ({ commit }: { commit: any }, view: any) {
    commit('ADD_CACHED_VIEW', view);
  },
  addView ({ dispatch }: { dispatch: any }, view: any) {
    dispatch('addVisitedView', view);
    dispatch('addCachedView', view);
  },

  // Delete the current route  delVisitedView ({ commit }: { commit: any }, view: any) {
    commit('DEL_VISITED_VIEW', view);
  },
  // Delete the current route name  delCachedView ({ commit }: { commit: any }, view: any) {
    commit('DEL_CACHED_VIEW', view);
  },
  delView ({ dispatch }: { dispatch: any }, view: any) {
    dispatch('delVisitedView', view);
    dispatch('delCachedView', view);
  },

  // Delete routes outside the current route  delOthersVisitedViews ({ commit }: { commit: any }, view: any) {
    commit('DEL_OTHERS_VISITED_VIEWS', view);
  },
  // Delete routes outside the current route name  delOthersCachedViews ({ commit }: { commit: any }, view: any) {
    commit('DEL_OTHERS_CACHED_VIEWS', view);
  },
  delOthersViews ({ dispatch }: { dispatch: any }, view: any) {
    dispatch('delOthersVisitedViews', view);
    dispatch('delOthersCachedViews', view);
  },

  // Delete all accessed routes  delAllVisitedViews ({ commit }: { commit: any }) {
    commit('DEL_ALL_VISITED_VIEWS');
  },
  // Delete all accessed routes 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
};

// 
const getters = {
  // The page tags visited  visitedViews: (state: any) =&gt; ,
  // The page tag to visit is named  cachedViews: (state: any) =&gt; 
};
export default getters;

router file

import Vue from 'vue';
import VueRouter from 'vue-router';
import Login from '@/views/login/';
import Layout from '@/layout/';

(VueRouter);

/**
  * hidden means whether it needs to appear in the side navigation bar, true means it does not need to be
  * isFirst means whether there is only one level of permissions, only appears in only one subset, and no other grandchildren sets
  * When permissions have multiple subsets or grandchild sets, first-level permissions need to add meta
  * The secondary permissions have a subset, and must also have meta
  */

// Basic routingexport const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () =&gt; import('@/views/redirect/')
      }
    ]
  },
  {
    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: () =&gt; import('@/views/dashboard/'),
        meta: {
          title: 'front page',
          icon: 'el-icon-location'
        }
      }
    ]
  }
];

// Dynamic routingexport const asyncRoutes = [
  {
    path: '/form',
    component: Layout,
    redirect: '/form/index',
    isFirst: true,
    children: [
      {
        path: 'index',
        name: 'Form',
        component: () =&gt; import('@/views/form/'),
        meta: {
          title: 'Form',
          role: 'form',
          icon: 'el-icon-location'
        }
      }
    ]
  },
  {
    path: '/editor',
    component: Layout,
    redirect: '/editor/index',
    meta: {
      role: 'editors',
      title: 'Total Rich Text',
      icon: 'el-icon-location'
    },
    children: [
      {
        path: 'index',
        name: 'Editor',
        component: () =&gt; import('@/views/editor/'),
        meta: {
          title: 'Rich text',
          role: 'editor',
          icon: 'el-icon-location'
        }
      },
      {
        path: 'two',
        name: 'Two',
        redirect: '/editor/two/three',
        component: () =&gt; import('@/views/editor/'),
        meta: {
          title: 'Second-level navigation',
          role: 'two',
          icon: 'el-icon-location'
        },
        children: [
          {
            path: 'three',
            name: 'Three',
            component: () =&gt; import('@/views/editor/'),
            meta: {
              title: 'Level 3 Navigation',
              role: 'three',
              icon: 'el-icon-location'
            }
          },
          {
            path: 'four',
            name: 'Four',
            component: () =&gt; import('@/views/editor/'),
            meta: {
              title: 'Level 3 Navigation 2',
              role: 'four',
              icon: 'el-icon-location'
            }
          }
        ]
      }
    ]
  },
  {
    path: '/tree',
    component: Layout,
    redirect: '/tree/index',
    isFirst: true,
    children: [
      {
        path: 'index',
        name: 'Tree',
        component: () =&gt; import('@/views/tree/'),
        meta: {
          title: 'Tree diagram',
          role: 'tree',
          icon: 'el-icon-location'
        }
      }
    ]
  },
  {
    path: '/excel',
    component: Layout,
    redirect: '/excel/index',
    isFirst: true,
    children: [
      {
        path: 'index',
        name: 'Excel',
        component: () =&gt; import('@/views/excel/'),
        meta: {
          title: 'Import and Export',
          role: 'excel',
          icon: 'el-icon-location'
        }
      }
    ]
  }
];

// The route that jumped in errorexport const error = [
  // 404
  {
    path: '/404',
    component: () =&gt; import('@/views/error/'),
    hidden: true
  },
  {
    path: '*',
    redirect: '/404',
    hidden: true
  }
];

const createRouter = () =&gt;
  new VueRouter({
    scrollBehavior: () =&gt; ({
      x: 0,
      y: 0
    }),
    routes: constantRoutes
  });

const router = createRouter();

// Refresh the routeexport function resetRouter () {
  const newRouter = createRouter();
  (router as any).matcher = (newRouter as any).matcher;
}

export default router;

Used on the page

&lt;!-- Generally used on the main layout page --&gt;
&lt;template&gt;
  &lt;div class="layout"&gt;
     ......
    &lt;el-container :class="{ hideSidebar: isCollapse }"&gt;
      ......
      &lt;!-- Subject content --&gt;
      &lt;el-main&gt;
        &lt;!-- Visited routing tags --&gt;
        &lt;tags-view&gt;&lt;/tags-view&gt;
        &lt;keep-alive :include="cachedViews"&gt;
          &lt;router-view :key="$" /&gt;
        &lt;/keep-alive&gt;
      &lt;/el-main&gt;
    &lt;/el-container&gt;
  &lt;/div&gt;
&lt;/template&gt;

&lt;script lang="ts"&gt;
import Vue from 'vue';
import { mapGetters } from 'vuex';
import SubMenu from '@/components/SubMenu/';
import MyBreadcrumb from '@/components/Breadcrumb/';
import TagsView from '@/components/TagsView/';
import { resetRouter } from '@/router';

export default ({
  computed: {
    //Route    ...mapGetters(['cachedViews'])
  },
  components: {
    TagsView
  }
});
&lt;/script&gt;

Summarize

Refer to online information for packaging and modification, and the specific requirements can be modified according to the project

The above is personal experience. I hope you can give you a reference and I hope you can support me more.