Vue Global Route Guard using beforeEach does not trigger(使用beforeEach的VUE全局路由保护不触发)
本文介绍了使用beforeEach的VUE全局路由保护不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的路由防护在calling it per-route使用beforeEnter
时工作,但在使用beforeEach
将其作为global route guard调用时不起作用。
在我顶部的代码中,您可以看到调用/dashboard
重定向时起作用的示例。
但如果我尝试使用beforeEach
在代码底部的所有路由上全局调用它,则它不起任何作用。
我做错了什么?
附注:我正在使用打字稿。
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;
推荐答案
router.beforeEach(() => { Auth0.routeGuard; })
无效,因为Auth0.routeGuard
函数实际上并未在该语句中被调用。调用函数时,通常使用圆括号将任何参数括起来(例如,Auth0.routeGuard(arg1, arg2, arg3)
)。
解决方案
需要使用router.beforeEach
中的navigation guard参数调用路由防护:
import { RouteLocationNormalized } from 'vue-router'
⋮
router.beforeEach((to: RouteLocationNormalized, from: RouteLocationNormalized, next: Function) => {
⋮
Auth0.routeGuard(to, from, next)
⋮
})
这篇关于使用beforeEach的VUE全局路由保护不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:使用beforeEach的VUE全局路由保护不触发


猜你喜欢
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06