On the problem of vue-router routing Guard

do another rights management module. The idea now is that when I log in, I directly generate the following code of router

.
  LoginByUsername({dispatch, commit, state}, loginForm) {
    const username = loginForm.username.trim()
    return new Promise((resolve, reject) => {
      loginByUsername(username, loginForm.password).then(response => {
        const data = response.result
        const meta = response.meta
        commit(SET_NAME, data.name)
        commit(SET_MENUFLAT, meta.flatMenus)
        commit(SET_MENUTREE, meta.treeMenus)
        /**/
        dispatch("generateRoutes").then((asyncRoute) => {
          console.log(state.addRouters)
          router.addRoutes(state.addRouters)
        })
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

here accessible routes are generated through router.addRoutes.
it"s OK to log in at first. After logging in, click on the menu bar to jump normally.

but if you press enter or refresh in the browser address column at this time, the route will fail
in

.

router.beforeEach((to, from, next) => {
  /*getters*/

  NProgress.start()
  /*token*/
  if (getToken()) {
    /*logn*/
    if (to.path === "/login") {
      next({path: "/"})
      NProgress.done()
    } else {
      next()
      NProgress.done()
    }
  } else {
    if (WhiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next("/login")
      NProgress.done()
    }
  }
})
The

breakpoint found that after refreshing the browser, the intercepted to here has no match matching address. Why

solve the puzzle

Mar.20,2021

the problem is that the addRoutes route fails after the page is refreshed, so judge and then regenerate the route

Menu