由于我使用 react-router 在 react 应用程序中处理我的路由,我很好奇是否有办法重定向到外部资源。
说有人打:
example.com/privacy-policy
我希望它重定向到:
example.zendesk.com/hc/en-us/articles/123456789-Privacy-Policies
我发现完全零帮助避免在我的 index.html 加载时用普通 JS 编写它,例如:
if ( window.location.path === "privacy-policy" ){
window.location = "example.zendesk.com/hc/en-us/articles/123456789-Privacy-Policies"
}
原文由 Eric Hodonsky 发布,翻译遵循 CC BY-SA 4.0 许可协议
我实际上最终构建了自己的组件。
<Redirect>
它从
react-router
元素中获取信息,因此我可以将其保留在我的路线中。如:这是我的组件以防万一 - 任何人都很好奇:
编辑——注意:
这是
react-router: 3.0.5
,在 4.x 中并不那么简单