如何在反应路由器 Dom v6 中导航到分隔 URL?

How to Navigate to separate URL in react router Dom v6?

我正在制作一个验证表单,具有检查密码和电子邮件的功能,如果为真,它应该导航到一个单独的网站(而不是网络中的路径)。但它不起作用

下面是我的代码:

function validate() {
    if (email==='123@123.com' && password==="123456"){
        console.log(email);
 history('http://www.dummy.com/webmail')
}
}

return{
.
.
.
<Button className={stylesAuth.submitButton} variant="warning" onClick={validate}>
}

我应该怎么做?

由于您要重定向到外部网站,您可以简单地使用普通的旧 JavaScript 并执行以下操作:

window.location.href = "https://www.dummy.com/webmail";

有关详细信息,请参阅 https://developer.mozilla.org/en-US/docs/Web/API/Location/href

更多信息:在这种情况下使用 history 没有意义,它仅用于您的 Web 应用程序中的内部导航。事实上,您甚至无法将外部 URL 推入历史堆栈:

The new URL must be of the same origin as the current URL; otherwise, pushState() will throw an exception. https://developer.mozilla.org/en-US/docs/Web/API/History/pushState