【问题标题】:ReactJS - /path/:id redirects with string :id instead of id valueReactJS - /path/:id 使用字符串重定向:id 而不是 id 值
【发布时间】:2019-10-31 18:52:36
【问题描述】:

我正在用 react-router 编写重定向规则。定义我的 路线 如下:

{
  path: '/inputs',
  component: InputsContainer,
  label: 'Inputs',
  icon: 'fal fa-mobile',
  menu: false,
  routes: [
    {
      path: '/inputs',
      component: () => <Redirect to="/om/inputs" />,
      index: true,
      exact: true
    },
    {
      path: '/inputs/:id',
      component: (location) => <Redirect exact from="/inputs/:id" to={`/om/orders/:id`} />,
      label: "Order Details",
      menu: false
    },
    notFoundRoute
  ]
}

在这里,/inputs 重定向到 /om/inputs 没有任何问题。但是 /inputs/23 被重定向到 /om/input/:id

我在这里缺少什么?我希望将其重定向到 /om/inputs/23

【问题讨论】:

    标签: reactjs redirect react-router


    【解决方案1】:

    您需要提供一个 url 而不是Redirect 的路径。由于您使用的是渲染道具模式,您可以使用match.params.id 获取它,其中match 是从回调函数参数中获得的。确保使用 render 而不是 Component 作为 Route 的道具

    {
      path: '/inputs',
      component: InputsContainer,
      label: 'Inputs',
      icon: 'fal fa-mobile',
      menu: false,
      routes: [
        {
          path: '/inputs',
          render: () => <Redirect to="/om/inputs" />,
          index: true,
          exact: true
        },
        {
          path: '/inputs/:id',
          render: ({match}) => <Redirect exact from="/inputs/:id" to={`/om/orders/${match.params.id}`} />,
          label: "Order Details",
          menu: false
        },
        notFoundRoute
      ]
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    • 2020-01-30
    • 1970-01-01
    • 2013-06-08
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多