Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tlerbao opened this issue Feb 22, 2024 · 0 comments
Open

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

tlerbao opened this issue Feb 22, 2024 · 0 comments

Comments

@tlerbao
Copy link

tlerbao commented Feb 22, 2024

我这两天写自动登录,在路由中,让这个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 };
    }
  }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant