由于我在 React 应用程序中使用 react-router 来处理我的路由,我很好奇是否有一种方法可以重定向到外部资源。
说有人打:
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"
}
最佳答案
这是使用 React Router 重定向到外部链接的单行代码:
<Route path='/privacy-policy' component={() => {
window.location.href = 'https://example.com/1234';
return null;
}}/>
它使用 React 纯组件概念将组件代码缩减为单个函数,而不是渲染任何内容,而是将浏览器重定向到外部 URL。
适用于 React Router 3 和 4。
关于javascript - React-Router 外部链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42914666/