通过 <Redirect/> 在新标签页中打开反应路由器的组件 [英] Opening in a new tab through <Redirect /> component of react router
问题描述
React Router Link 组件接受 target="_blank"
以在新选项卡中打开页面.有没有办法对重定向组件做同样的事情?
React Router Link component takes in target="_blank"
to open the page in a new tab. Is there a way to do the same with the Redirect component?
我的组件的设置方式,我无法将 Link
包裹在有问题的组件周围(组件上有一个按钮,点击它也会重定向,我不想要) 这就是为什么我使用 Redirect
代替,但找不到在新选项卡中打开新 URL 的方法,怎么做?
The way my components are set up, I can't wrap Link
around the component in question (there is a button on the component, clicking on which also redirects, which I don't want) that's why I'm using Redirect
instead, but can't find a way to open the new URL in a new tab, How can it be done?
推荐答案
有没有办法对重定向组件做同样的事情?
Is there a way to do the same with the Redirect component?
简答:
没有
有点长:
技术上 为您创建一个
标签,该标签与
target="_blank"
一起使用.来自文档:
Technically <Link>
creates an <a>
tag for you which works with target="_blank"
. From the docs:
在您的应用程序周围提供声明性的、可访问的导航.
Provides declarative, accessible navigation around your application.
正如文档所述,<Redirect>
用于其他用途:
<Redirect>
is used for something else, as the documentation states:
渲染
将导航到新位置.新位置将覆盖历史堆栈中的当前位置,就像服务器端重定向 (HTTP 3xx) 所做的那样.
Rendering a
<Redirect>
will navigate to a new location. The new location will override the current location in the history stack, like server-side redirects (HTTP 3xx) do.
总之,
正在更新历史记录, 用作锚标记.在一天结束时,如果你真的需要使用
target="_blank"
那么我建议使用 或创建一个
;
标记用于此目的.
So in summary <Redirect>
is updating the history, <Link>
is used as an anchor tag. At the end of the day if you really need to use target="_blank"
then I would suggest to use <Link>
or create an <a>
tag for that purpose.
之前我创建了一个代表重定向选项的 GitHub 存储库,请在下面找到它:https://github.com/norbitrial/react-router-programmatically-redirect-例子
Earlier I have create a GitHub repository which represents redirect options, please find it below: https://github.com/norbitrial/react-router-programmatically-redirect-examples
希望能帮到你!
这篇关于通过 <Redirect/> 在新标签页中打开反应路由器的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!