Geeker-Admin icon indicating copy to clipboard operation
Geeker-Admin copied to clipboard

Vue RFC讨论中已经去除路由的 next

Open tlerbao opened this issue 6 months ago • 0 comments

我这两天写自动登录,在路由中,让这个next搞的焦头烂额各种警告

最后看文档,已经移除但还支持,所以改了写法,完全不用next,世界清净了,我擦;好用的一B。

CleanShot 2024-02-22 at 20 18 10@2x

https://router.vuejs.org/zh/guide/advanced/navigation-guards.html https://github.com/vuejs/rfcs/blob/master/active-rfcs/0037-router-return-guards.md#motivation

// 下面如果去除我自动登录的代码,GeekAdmin的也可以更简洁
router.beforeEach(async (to: toRouteType, from) => {
  const userStore = useUserStore();

  // 1.NProgress 开始
  NProgress.start();

  // 2.动态设置标题
  const title = import.meta.env.VITE_GLOB_APP_TITLE;
  document.title = to.meta.title ? `${to.meta.title} - ${title}` : title;

  // 3.判断是访问登陆页,有 Token 就在当前页面,没有 Token 重置路由到登陆页
  if (to.path.toLocaleLowerCase() === LOGIN_URL) {
    if (userStore.token) return { path: from.fullPath, replace: true };
  }

  // 4.判断访问页面是否在路由白名单地址(静态路由)中,如果存在直接放行
  if (!ROUTER_WHITE_LIST.includes(to.path)) {
    // 5.判断是否有 Token,没有重定向到 login 页面
    if (!userStore.token && to.path.toLocaleLowerCase() !== LOGIN_URL) {
      // 6.企业微信自动OAuth登录
      if (isWeWork()) {
        return await userStore.loginWeWork();
      }
      return { path: LOGIN_URL, replace: true };
    }
  }
});

tlerbao avatar Feb 22 '24 12:02 tlerbao