Reasons and solutions for failure of dynamically added routing pages in Vue when refreshing

Reasons and solutions for failure of dynamically added routing pages in Vue when refreshing

Problem Description

Yesterday, when I was working on the function of dynamically adding permission pages to routes in the Vue backend management system, I encountered a problem: the dynamically added routing page had a 404 error when the page was refreshed.

Scenario

The permission control of the backend management system is achieved by defining the permission code on the frontend page, saving the code to the backend colleagues and configuring it in a table. Then, the permission code list returned by the backend is filtered and matched with the code menu list configured on the frontend page. Pages with equal codes are permissioned pages, and then dynamically added to the route through router.addRoute(). Only routes with permission can be accessed, otherwise it will prompt that there is no permission.

Fixed routes are placed in the new Router at the beginning, such as the login page login

Interface return

Front-end menu definition

Methods in vuex

Problems

After logging in, call the method in vuex to complete the acquisition of permission code, dynamically filter the permission routing page operation, and then add the permission menu to the route through router.addRoute(), enter the dynamically added routing page, and refresh the page to display 404

Cause Analysis

When the page is refreshed, the routes are reinitialized. The dynamically added routes no longer exist. Only some fixed routes (such as the login page) are still there, so a 404 error occurs.

Solution

The data stored in the VUEX store will be cleared when the page is refreshed.
Make a judgment at the global navigation router.beforeEach of the route, and judge whether the page is refreshed according to whether the list stored in VUEX has a value. If it is not 0, it is the first login. After login, the matching route will be used, and there will be no problem; if list.length is 0, it is a refresh page, and you need to re-execute the route matching and re-add the dynamic route.

Implement code route/index.js to add logic judgment to the navigation guard

———router.js————-

const constantRoutes = [
 {
  path: '/',
  redirect: '/login'
 },
 {
  path: '/login',
  name: 'login',
  meta: {
   auth: false
  },
  component: () => import('@/views/login')
 },
 {
  path: '/layout',
  name: 'layout',
  meta: {
   auth: true
  },
  component: () => import('@/views/layout/index'),
  children: [
   {
    path: '/index',
    name: 'index',
    component: () => import('@/views/home')
   }
  ]
 },
 {
  path: '*',
  component: () => import('@/views/error/404')
 }
]

Vue.use(VueRouter)
const createRouter = () =>
 new VueRouter({
  routes: constantRoutes
 })
export function resetRouter() {
 const newRouter = createRouter()
 router.matcher = newRouter.matcher // reset router
}
const router = createRouter()
 
//Reset the permission page dynamic routing after the page is refreshed to prevent dynamic routing 404 problems const reSetPermissionList = to => {
 return new Promise((resolve, reject) => {
  if (to.path !== '/login' && store.state.permission.permissionList.length === 0) {
   store
    .dispatch('permission/getPermissionList')
    .then(() => {
     resolve('permCode')
    })
    .catch(error => {
     resolve('permCode')
    })
  } else {
   resolve()
  }
 })
}
router.beforeEach((to, from, next) => {
  
 const accessToken = localStorage.getItem('accessToken')
 if (_.isEmpty(accessToken)) {//Are you logged in? Go to the login page next({
   path: '/login',
   query: {
    redirect: to.fullPath
   }
  })
 } else { //Logged in user enters the page if (to.path === '/login') {
   next({ path: '/index' })
  } else {
    reSetPermissionList(to).then(data => {
     data === 'permCode' ? next({ path: to.path, query: to.query }) : next()
    })
  }
 }
 
})

Summarize

It mainly determines whether the data in VUEX exists in the global navigation and whether the page is refreshed. If so, it goes through the permission routing matching method again.

The above is the detailed content of the reasons and solutions for the failure of the dynamically added routing page of Vue to refresh. For more information about Vue routing page refresh, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Solve the problem that the Vue single page does not refresh when using keep-alive
  • How to implement vue2.0 page forward refresh back without refreshing
  • Vue/react single page application back without refresh solution
  • Vue does not refresh when returning to the previous page and its solution

<<:  How to deploy tomcat in batches with ansible

>>:  Detailed discussion of the character order of mysql order by in (recommended)

Recommend

Solution to garbled display of Linux SecureCRT

Let's take a look at the situation where Secu...

Vue2.x configures routing navigation guards to implement user login and exit

Table of contents Preface 1. Configure routing na...

Detailed explanation of execution context and call stack in JavaScript

Table of contents 1. What is the execution contex...

Detailed explanation of creating, calling and managing MySQL stored procedures

Table of contents Introduction to stored procedur...

The phenomenon of margin-top collapse and the specific solution

What is margin-top collapse Margin-top collapse i...

Two solutions for automatically adding 0 to js regular format date and time

Table of contents background Solution 1 Ideas: Co...

How to limit the number of records in a table in MySQL

Table of contents 1. Trigger Solution 2. Partitio...

Ant designing vue table to achieve a complete example of scalable columns

Perfect solution to the scalable column problem o...

Mysql join table and id auto-increment example analysis

How to write join If you use left join, is the ta...

Use personalized search engines to find the personalized information you need

Many people now live on the Internet, and searchin...

What is Software 404 and 404 Error and what is the difference between them

First of all, what is 404 and soft 404? 404: Simpl...

Solve the error problem caused by modifying mysql data_dir

Today, I set up a newly purchased Alibaba Cloud E...

MySQL 5.7.23 version installation tutorial and configuration method

It took me three hours to install MySQL myself. E...

Using Vue to implement timer function

This article example shares the specific code of ...

How to install grafana and add influxdb monitoring under Linux

Install grafana. The official website provides an...