【问题标题】:react router default URL address反应路由器默认 URL 地址
【发布时间】:2022-01-12 13:37:43
【问题描述】:

我想在 react router 中更改我的默认 URL 地址。

我该怎么做?

例如在我的项目中,我的默认 URL 是 http://localhost:3000

但在我的部分代码中,我想将我的用户发送到另一个 URL,例如:https://www.google.com/

代码:

const history = useHistory();

const handleGo = () => {
     history.push(`https://www.google.com`)
  };

return (
    <button onClick={handleGo} >
         GO
    </button>
)

当用户点击按钮 我想将 URL 更改为 https://www.google.com 但它更改为 http://localhost:3000/https://www.google.com

【问题讨论】:

  • 要导航到应用程序外部的链接,您应该使用 html &lt;a&gt; 或 js window.location object
  • 只需使用a href 进行外部重定向,&lt;a href="https://www.google.com" target="_blank"&gt;GO&lt;/a&gt;
  • @PedroFeltrin 我想也许有办法使用 react-router。谢谢!我使用 window.location
  • @KaungKhantZaw 我从后端得到一个 URL 并在新的 href 中使用它,你的方式没有回答。谢谢

标签: reactjs react-router react-router-dom


【解决方案1】:
react-router 中的

history.push 仅将新条目推送到历史堆栈中。这会导致您的网址被附加。

要重定向到新页面,您可以在&lt;a&gt; 标签中使用href

例如:&lt;a href="https://www.google.com" target="_blank" rel="noopener noreferrer"&gt;go&lt;/a&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-25
    • 2021-11-08
    • 1970-01-01
    • 2018-01-14
    • 1970-01-01
    • 2018-01-04
    • 2021-04-17
    • 1970-01-01
    相关资源
    最近更新 更多