如何 vue 路由器使用动态参数需要匹配?

how to vue router use dynamic params require match?

我在路由器中定义了一个动态参数。这个参数接收一定的参数值即使是动态的。

示例:

{
  path: '/:type',
  component: List,
}

路由器可以采用 2 或 3 个参数。例如,它可以接受的参数是 'actual' 和 'archive'

当用户键入除“://localhost:8080/actual”或“://localhost:8080/[=”以外的参数时,我想重定向到 404 19=]存档'。我可以在 .vue 文件中执行此操作,但我想在路由器文件中进行调整。

您可以在路由配置本身上设置 Per-Route Guard

{
  path: '/:type',
  component: List,
  beforeEnter: (to, from, next) => {
    //check the params
    if(to.params.type === 'actual' ||to.params.type === 'archive'){
      next();
    }else{
      next('/404');//error route
    }
  }
}