我有一个路由守卫,在使用每个路由调用它beforeEnter
时可以工作,但在使用.beforeEach
在顶部的代码中,您可以看到调用/dashboard
重定向时有效的示例。
但是,如果我尝试在代码底部的所有路由上全局调用它,则使用beforeEach
它什么也不做。
我究竟做错了什么?
PS 我正在使用 TypeScript。
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import store from "@/store";
import { Mutations, Actions } from "@/store/enums/StoreEnums";
import { Auth0 } from "@/auth";
const routes: Array<RouteRecordRaw> = [
{
path: "/",
redirect: "/dashboard",
component: () => import("@/layout/Layout.vue"),
//beforeEnter: Auth0.routeGuard, // <--- THIS WORKS
children: [
{
path: "/dashboard",
name: "dashboard",
component: () => import("@/views/Dashboard.vue"),
},
{
path: "/add-post",
name: "add-post",
component: () => import("@/views/Builder.vue"),
},
],
},
{
// the 404 route, when none of the above matches
path: "/404",
name: "404",
component: () => import("@/views/crafted/authentication/Error404.vue"),
},
{
path: "/:pathMatch(.*)*",
redirect: "/404",
},
];
const router = createRouter({
history: createWebHashHistory(),
routes,
});
router.beforeEach(() => {
store.commit(Mutations.RESET_LAYOUT_CONFIG);
Auth0.routeGuard; // <--- THIS DOES NOT WORK
store.dispatch(Actions.VERIFY_AUTH);
// Scroll page to top on every route change
setTimeout(() => {
window.scrollTo(0, 0);
}, 100);
});
export default router;