【问题标题】:how can i add query in react router我如何在反应路由器中添加查询
【发布时间】:2020-09-08 06:55:17
【问题描述】:

现在是

http://localhost:8001/components/button/

我想要

http://localhost:8001/components/button/?theme=dark

我用过

browserHistory.push({
  pathname: '',
  query: {theme: dark},
})

and

browserHistory.push({
  pathname: 'components/button',
  query: {theme: dark},
})

and 

browserHistory.push({
  pathname: 'button',
  query: {theme: dark},
})

全部失败。

我该怎么办?能帮帮我吗,谢谢!

【问题讨论】:

  • 你不能只做browserHistory.push("/components/button/?theme=dark")或试试browserHistory.push({ pathname: 'components/button', search: '?theme: dark', })
  • @dikuw 结果是 localhost:8001/components/button/components/button/?theme=dark

标签: reactjs routes react-router


【解决方案1】:

您可以只使用 string literal 并推送历史记录。

browserHistory.push(`components/button?theme=${dark}`)

【讨论】:

【解决方案2】:

您可以将push 方法用作:

browserHistory.push({
  pathname: '/components/button',
  search: '?theme=dark'
})

或简单如下:

browserHistory.push('/components/button?theme=dark')

【讨论】:

  • 它将是 /components/button/components/button/?theme=dark
  • 这是 /components/button/ ,而不是 /components/button 。所以它不起作用。
猜你喜欢
  • 2016-01-01
  • 1970-01-01
  • 2018-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-06
  • 2017-06-26
  • 2016-07-06
相关资源
最近更新 更多