【问题标题】:VueJS: How to access parameter value inside routes fileVueJS:如何访问路由文件中的参数值
【发布时间】:2021-05-31 03:00:16
【问题描述】:

我在 routes.js 中有这条路线

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

如果用户未登录,他将被重定向到登录页面,但在此之前我想检查参数是否已设置(例如 user_id)并在进入登录页面之前将其存储。如何在此路径内的 routes.js 文件中执行此操作?

【问题讨论】:

  • 你指的是哪个参数?
  • user_id, server_id ..

标签: vue.js routes store


【解决方案1】:

假设您有一个名为 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
   }
 }
},

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 2018-07-04
    • 2018-12-22
    • 1970-01-01
    • 1970-01-01
    • 2022-06-29
    • 2020-09-25
    • 2021-09-24
    • 1970-01-01
    相关资源
    最近更新 更多