reactjs - 即使没有提供路径参数,也路由到相同的 React 组件
问题描述
即使没有提供路径参数,我也试图在我的应用程序中呈现相同的组件:
<Switch>
<Route
path="/productInfoList/:product"
component={ProductInfoList}
/>
</Switch>
但是,如果 URL 中没有提供 ":product" 参数,则不会呈现组件。即使没有提供路径参数,有没有办法呈现相同的“ProductInfoList”组件?
我觉得必须有一种有效的方法来做到这一点,而不是path="/productInfoList"
完全重新声明一条新路线。
解决方案
您好,您可以选择没有婴儿车的路线,然后传入 history.push 或链接
history.push({
pathname: '/template',
search: '?query=abc',
state: { detail: response.data }
})
<Link to={{
pathname: '/template',
search: '?query=abc',
state: { detail: response.data }
}}> My Link </Link>
正如你在这里看到的,如果你想获取数据,你可以传递一个状态,你可以像这样使用 props.location.state.detail 你可以跟踪你是否得到一个状态,然后按照你想要的方式使用它
推荐阅读
- json.net - 如何返回序列化的 JSON 字符串,而无需从 ASP.Net Core API 进一步序列化
- amazon-web-services - 在 VPN 后面使用 AWS Glue
- python - Python子进程运行跳过提示
- nginx - 502 错误网关 nginx 虚拟机
- java - Eclipse Javadoc 背景覆盖默认值
- c - 在 C 语言中使用 switch case 执行 while 循环不起作用
- html - 无法将文本居中对齐
- go - Golang gorename 工具失败
- powershell - Powershell“Get-MailboxImportRequest | Remove-MailboxImportRequestStatistics”列表
- json - 如何使用 Alamofire 和 swift 4 为 Clarifai API 编写 POST 请求?