SoFunction
Updated on 2025-04-03

vue addRoutes route dynamic loading operation

need:Add permission control to realize different route navigation for different roles

Ideas:After each login, request the interface to return to the current role route

Core method:vue-router2.2.0 addRoutes method + vuex

The following is the method I implemented to get menu routes. I put the call of this method in the life hook of the homepage component. Even if the user refreshes the browser and clears the route, it will still call the interface to get again, and it will not be lost. At the same time, considering that there is a possibility of switching users, the obtained routing information is not saved to cookies or localstorage

Before obtaining the menu, we will first judge the routerState to avoid multiple requests. I use the v-for loop parameter of element-ui to display dynamic routing navigation

/**
 * Get menu
 */
getMenu () {
 if (this.$ === false) {
 // Clean up existing dynamic routes ()
 // Change the request routing status to true this.$('SET_ROUTERSTATE', true)
 getMyMenu().then((res) => {
  if ( === '0') {
  // Format the route and convert the data into an array of route formats that addRoutes is acceptable  let myMenu = ()
  if ( <= 0) { // No dynamic routing   return
  }
  for (let index = 0; index < ; index++) {
   //Storing the requested route into the options object first   this.$(myMenu[index])
  }
  // Add the complete routes to be displayed  this.$(this.$)
  // Here the route will be displayed on the page   = this.$
  }
  // Here you can print out a new route  (this.$router)
 })
 }
}

Supplementary knowledge:vue+element enters different routing pages (secondary pages) and lets the corresponding left-hand menu

Routing configuration

{
 path: '/finance',
 name: 'Finance',
 meta: {
 title: 'finance'
 },
 component: () =>
 import('@/components/Finance'),
 redirect: '/finance/code/code',
 children: [{
 path: '/finance/code',
 name: 'financeindex',
 meta: {
 title: 'Tax allocation'
 },
 redirect: '/finance/code/code',
 component: () =>
 import('@/components/finance/financeindex'),
 children: [{
 path: '/finance/code/code',
 name: 'FinanceCode',
 hidden: false,
 active:'/finance/code', //This is displayed in the menu on the left and needs to be selected meta: {
  title: 'Tax code (gold tax)'
 },
 component: () =>
  import('@/components/finance/code/Code'),
 },
 {
 path: '/finance/code/codeimportrecord',  // The interface entered by this route is the second-level page of tax code (gold tax). When entering this page, the tax code (gold tax) in the menu needs to be displayed and selected. name: 'FinanceCodeImportRecord',
 hidden: true,
 meta: {
 title: 'Tax code import record'
 },
 component: () =>
 import('@/components/finance/code/CodeImportRecord'),
 },
 {
 path: '/finance/classcode/classcode',
 name: 'FinanceClassCode',
 hidden: false,
 active:'/finance/classcode', //To save trouble, only add active attributes to the routes that need to be displayed on the left side. meta: {
  title: 'Classification Tax Code Confirmation'
 },
 component: () =>
  import('@/components/finance/classCode/ClassCode'),
 },
 ]
 }, ]

element

<template>
 <div class="leftnav">
 <!--<div class="" v-for="nav in navs">
   <div class="LiHeader">
    {{}}
   </div>
   <li v-for="item in ">
    {{}}
   </li>
  </div>-->
 <el-menu
  style="text-align: left;"
  :default-active= // The value of this refers to the day corresponding to the value of index in el-menu-item: selected (normal situation is a route for each page. If you enter that route, the corresponding navigation menu needs to be selected)  class="el-menu-vertical-demo"
  @open="handleOpen"
  @close="handleClose"
  background-color="#282b33"
  text-color="#bcbcbc"
  active-text-color="#ffffff">
  <div class="" v-for="(nav,index) in navs" :key="index" style="width: 200px;">
  <div class="" style="color: #ffffff;height: 40px;line-height: 40px;padding-left: 20px;font-size: 16px;">
   {{}}
  </div>
  <el-menu-item @click="clickroute()" v-for="(item,index) in " v-if="!" :key="index" :index=""(It turns out to be) style="height: 40px;line-height: 40px;">{{}}</el-menu-item>
  </div>

 </el-menu>
 </div>
</template>
<script>

js

data() {
  return {
  navs:[],
  show:null //Initialize the above: default-active bound value  }
 },
 created() { //// When loading the page, get the url, use / to disassemble it, then put together the first two paths and assign values. At this time, the show corresponds to the avtive in the routing table.  let route=this.$('/')
  let vueRouter=this.$(router=>{return =='/'})[0].children
  let filterVuerouter=(router=>{return '/'+route[1] ==  })
  =filterVuerouter[0].children
  ()

  let router ='/'+route[1]+'/'+route[2]
 (router)
  =router
//  ()
 },
 mounted() {

 },

The above article on vue addRoutes routing dynamic loading is all the content I share with you. I hope you can give you a reference and I hope you can support me more.