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

How to access Vuex store in VueRouter.beforeEach in TypeScript? #1873

Closed
ShenQingchuan opened this issue Nov 16, 2020 · 3 comments
Closed

How to access Vuex store in VueRouter.beforeEach in TypeScript? #1873

ShenQingchuan opened this issue Nov 16, 2020 · 3 comments
Labels
need repro Reproduction code is required

Comments

@ShenQingchuan
Copy link

ShenQingchuan commented Nov 16, 2020

What problem does this feature solve?

I want to set a guard in VueRouter.beforeEach(), it will redirect to the login page when the loggedIn state is false.

What does the proposed API look like?

I want to achieve something this this:

import { RouteRecordRaw, createRouter, createWebHistory } from "vue-router";
import store from "@/store";

const routes: Array<RouteRecordRaw> = [
  {
    path: "/login",
    name: "Login",
    component: () => import("../views/Login.vue")
  },
  {
    path: "/",
    name: "Home",
    component: () => import("../views/Home.vue"),
    meta: {
      requiresAuth: true
    }
  },
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
});

router.beforeEach((to, from, next) => {
  const loggedIn = store.state.authentication.loggedIn;
  if (to.matched.some(record => record.meta.requiresAuth) && !loggedIn) {
    next({
      name: "Login",
      query: { redirect: to.fullPath }
    });
  } else {
    next();
  }
});

export default router;
@ShenQingchuan
Copy link
Author

just similar to #1841 , and that issue hang on for a long while...

@kiaking
Copy link
Member

kiaking commented Nov 16, 2020

Sorry what problem are you having at the moment? Are you getting Object is possibly null error? Either way, please provide reproduction code so that we can test this 🙏 Not just this single file, but we need whole project.

@kiaking kiaking added the need repro Reproduction code is required label Nov 16, 2020
@alpha-hxb
Copy link

What problem does this feature solve?

I want to set a guard in VueRouter.beforeEach(), it will redirect to the login page when the loggedIn state is false.

What does the proposed API look like?

I want to achieve something this this:

import { RouteRecordRaw, createRouter, createWebHistory } from "vue-router";
import store from "@/store";

const routes: Array<RouteRecordRaw> = [
  {
    path: "/login",
    name: "Login",
    component: () => import("../views/Login.vue")
  },
  {
    path: "/",
    name: "Home",
    component: () => import("../views/Home.vue"),
    meta: {
      requiresAuth: true
    }
  },
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
});

router.beforeEach((to, from, next) => {
  const loggedIn = store.state.authentication.loggedIn;
  if (to.matched.some(record => record.meta.requiresAuth) && !loggedIn) {
    next({
      name: "Login",
      query: { redirect: to.fullPath }
    });
  } else {
    next();
  }
});

export default router;

Hello,is this problem solve?I have the same problem ,can you tell me the sln? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need repro Reproduction code is required
Projects
None yet
Development

No branches or pull requests

3 participants