【发布时间】: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
<a>或 jswindow.locationobject -
只需使用
a href进行外部重定向,<a href="https://www.google.com" target="_blank">GO</a> -
@PedroFeltrin 我想也许有办法使用 react-router。谢谢!我使用 window.location
-
@KaungKhantZaw 我从后端得到一个 URL 并在新的 href 中使用它,你的方式没有回答。谢谢
标签: reactjs react-router react-router-dom