带有可选查询参数的 Angular 路由器

Vla*_*iuk 3 router angular

我有路由器

{
  path: 'reset-password',
  component: ResetPasswordComponent,
}
Run Code Online (Sandbox Code Playgroud)

并且我希望能够将其作为直接链接 ( /reset-password) 或使用传递的参数 ( /reset-password?uid=gsSxc&code=DsdxFSd)打开,因此在创建路由器快照时我想查看传递的参数。

我怎样才能做到这一点?我需要为此创建两个不同的路由器吗?

Vol*_*hat 5

您不需要单独的路由,因为所有查询参数始终是可选的,您应该在组件内部处理必需的参数。

或者path: 'reset-password/:uid',通过查询字符串在 url 中定义所需的参数为 和路径可选参数reset-password/1234?queryString=true

  • 您也可以在路线守卫中处理它们。 (2认同)