流浪のwolf

卷帝

导航

如何重置路由实例

首先先定义一个创建路由实例的函数:

// 定义一个创建路由的函数
const createRouter = () =>
  new Router({
    mode: "hash",
    // mode: "history", // require service support
    // base: "/hr", // 路由基准地址   这是历史模式使用的 为了防止后端路径冲突
    scrollBehavior: () => ({ y: 0 }),
    // routes: constantRoutes, // constantRoutes静态路由映射
    // routes: [...constantRoutes, ...asyncRoutes], // 静态+动态 放在一起
    routes: [...constantRoutes], // 只有静态  放在一起   动态路由后i按根据用户权限控制添加动态路由
  });

然后

// 重置路由实例的方法
// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter(); // 重新创建一个路由实例
  router.matcher = newRouter.matcher; // 将新的路由实例的matcher属性赋给原来路由实例的matcher属性,即可实现原路由实例重置
}

 

posted on 2023-02-13 21:32  流浪のwolf  阅读(70)  评论(0编辑  收藏  举报