VueJS:如何访问路由文件中的参数值



我在routes.js中有这个路由

{
path: "/dashboard/:user_id?/:entity_id?/:server_id?",
name: "Dashboard",
component: () => import("./views/dashboard"),
meta: {
authRequired: true,
},
},

如果用户没有登录,他将被重定向到登录页面,但在此之前,我想检查参数是否已经设置(例如user_id),并在进入登录页面之前存储它。我怎么能做到这一点在路由。js文件在这个路径?

假设您有一个名为authenticationGuard的保护,检查用户是否登录,您可以这样做:

export function authenticationGuard(to, from) {
if (to.params.user_id) {
// do whatever you would like with `user_id` here
}
}

如果你不控制authenticationGuard,你可以在路由中插入routeGuard:

{
path: "/dashboard/:user_id?/:entity_id?/:server_id?",
name: "Dashboard",
component: () => import("./views/dashboard"),
meta: {
authRequired: true,
},
beforeEnter: (to, from) {
if (to.params.user_id) {
// do whatever you would like with user_id here
}
}
},

相关内容

  • 没有找到相关文章

最新更新